1

Possible Duplicate:
Undefined reference error for template method
Splitting templated C++ classes into .hpp/.cpp files--is it possible?

I have this class:

matrix.h

#ifndef MATRIX_H
#define MATRIX_H
#include <vector>
#include <iostream>

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

template <class T>
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<T>&);
        virtual ~matrix();
    private:
        vector<T> elements_;
        int dimension_;
};

#endif // MATRIX_H

#include "matrix.h"
#include <vector>

matrix.cpp

template <class T>
matrix<T>::matrix(const int& n)
{
    dimension_ = n;
}

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

template <class T>
int matrix<T>::size()
{
    return elements_.size()/dimension_;
}

template <class T>
T& matrix<T>::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 <class T>
ostream& operator << (ostream& o, const matrix<T>& 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 <class T>
matrix<T>::~matrix()
{
    //dtor
}

And this program:

#include "matrix.h"
#include <iostream>

using namespace std;

int main()
{
    matrix<int> 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?

Community
  • 1
  • 1
Heathcliff
  • 3,048
  • 4
  • 25
  • 44
  • 1
    http://stackoverflow.com/questions/1111440/undefined-reference-error-for-template-method?rq=1 – Vaughn Cato Oct 07 '12 at 22:19
  • This is one of the few "serious" questions with a million duplicates -- somehow it must be very difficult to search for this. – Kerrek SB Oct 07 '12 at 22:20
  • @KerrekSB Yeah. I was just going to say something like that. The good news is, apparently colleges are picking up C++ _with `templates`_ in their curricula these days. That has been worse – sehe Oct 07 '12 at 22:22
  • 1
    You already asked [the same question](http://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor) earlier! Why ask it again? – Kerrek SB Oct 07 '12 at 22:32
  • @KerrekSB Because I'm an idiot! I forgot about templates and C++. Sorry people. – Heathcliff Oct 07 '12 at 22:34

0 Answers0