0

Hi I'm learning templates in C++ so I decied to write matrix class which would be a template class. In Matrix.h file I wrote

 #pragma once
#include "stdafx.h"
#include <vector>



using namespace  std;

template<class T>
class Matrix
{
public:

    Matrix();

    ~Matrix();
    GetDataVector();
    SetDataVector(vector<vector<T>> dataVector);
    bool operator == (Matrix* matrix);
    bool operator < (Matrix* matrix);
    bool operator <= (Matrix* matrix);
    bool operator > (Matrix* matrix);
    bool operator >= (Matrix* matrix);
    Matrix* operator + (Matrix* matrix);
    Matrix* operator - (Matrix* matrix);
    Matrix* operator * (Matrix* matrix);

private:
    vector<vector<T>>  datavector;
    int columns,rows;


};

In Matrix cpp Visual Stuio automaticlly generated code for default constructors

#include "StdAfx.h"
#include "Matrix.h"


Matrix::Matrix()
{
}



Matrix::~Matrix()
{
}

However if I want to compile this I get an error

'Matrix' : use of class template requires template argument list The error are in file Matrix.cpp in default constructors What may be the problem ??

  • 1
    You want `bool operator == (const Matrix& matrix) const;` instead of `bool operator == (Matrix* matrix);`. Also, there is no need for a destructor since `std::vector` cleans up after itself. – fredoverflow Feb 06 '11 at 15:24
  • You can also look at the source code of armadillo (http://arma.sourceforge.net/download.html ). The source is clear, and the library is great. It is by the way the only actively maintained decent C++ linear algebra library. – Alexandre C. Feb 06 '11 at 15:55

3 Answers3

4

You will have to write your out of class function implementations as:

template <typename T>
Matrix<T>::Matrix() {}

template <typename T>
Matrix<T>::~Matrix() { }
hkaiser
  • 11,403
  • 1
  • 30
  • 35
1

You cannot put definition of templates classes or methods to other file, as linker won't link it (in theory export exists, but no compiler implements it). You can put it to other file and then include it after template declaration:

template<class T>
class Matrix
{
// (...) methods declarations here
};

#include "matrix_implementation.hpp"

Also don't use using namespace std; directive in header file, because it'll propagate to all files where it's included.

Pawel Zubrycki
  • 2,703
  • 17
  • 26
  • 1
    You mean `export`, not `extern`. – Puppy Feb 06 '11 at 15:45
  • You *can* put both the implementation and template instantiation in the .cpp file. For classes such as a matrix (which might have a limited number of valid instantiations), removing the implementation from the header can reduce compile/link times significantly since files including the .h do not need to all (re)compile the matrix implementations. To avoid the missing linker symbols, explicitly instantiate the template in the .cpp (template class Matrix;). The pattern is often also used to reduce multiple instantiations to a common implementation T* -> void*. – user48956 Feb 06 '11 at 21:34
0

Even when you fix the compilation error (see hkaiser's response), you'll run into linker errors, because of the way you organize your template code. See c++ class with templates compilation error, for instance

Community
  • 1
  • 1
Nemanja Trifunovic
  • 24,346
  • 3
  • 50
  • 88