Wednesday, 6 September 2017

Undefined reference C++ with template class







I have this class:



matrix.h



#ifndef MATRIX_H
#define MATRIX_H
#include
#include

using namespace std;
template class matrix;
template ostream& operator<< (ostream& o, const matrix& m);

template
class matrix
{
public:
matrix(const int&);
void push(const T&);
void setRows(const int&);
int size();
T& operator ()(const int&, const int&);
friend ostream& operator<< <>(ostream&, const matrix&);
virtual ~matrix();
private:
vector elements_;
int dimension_;
};

#endif // MATRIX_H

#include "matrix.h"
#include


matrix.cpp



template 
matrix::matrix(const int& n)
{
dimension_ = n;
}

template
void matrix::push(const T& element)
{
elements_.resize( elements_.size() + 1, element);
}

template
int matrix::size()
{
return elements_.size()/dimension_;
}

template
T& matrix::operator ()(const int &n, const int &m)
{
if ((n < 0) || (n > dimension_))
{
cerr << "Row index out of range"
<< endl << endl;
}
if ((m < 0) || (m > dimension_))
{
cerr << "Column index out of range"
<< endl << endl;
}

return elements_[n*dimension_+m];
}

template
ostream& operator << (ostream& o, const matrix& m)
{
for (int i = 0; i < m.size(); i++)
{
for (int j = 0; j < m.size(); i++)
{
o << m(i,j) << " ";
}
o << endl;
}
return o;
}


template
matrix::~matrix()
{
//dtor
}

And this program:

#include "matrix.h"
#include

using namespace std;

int main()
{
matrix m(3);
m.push(10);
m.push(10);
m.push(11);
m.push(11);
m.push(12);
m.push(12);
m.push(13);
m.push(13);
m.push(10);
cout << m;
cout << "Hello world!" << endl;
return 0;
}


And for EACH. SINGLE. ONE. Of the methods I call, I get a compiler error like this one:




C:\Users...\main.cpp|8|undefined reference to
`matrix::matrix(int const&)'|




I have the matrix.cpp file built. I'm using Code::Blocks, so I have a matrix.o file in the obj folder. That's not the problem.



What is it?

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...