Implementing Constructor of Generic Class

Dec 22, 2008 at 2:14am
Hi, I'm trying to implement the code for the constructor of the following class, outside it:

1
2
3
4
5
6
7
8
9
10
template <class T> 
class Matrix {
   	
	int w, h;
	T* m;

  public:
	Matrix (int _w, int _h);
    int get_width ();
};

As,

1
2
3
4
5
template <class T>
Matrix<T>::Matrix (int _w, int _h){
	w = _w;
	h = _h;
}


But it won't work! Anyone knows how to solve this problem? (I'm using VS08)


Thanks!
Dec 22, 2008 at 3:04am
I had a similar problem earlier that got resolved. Are you splitting your code into header and source? If so, you should put everything in a header file; it's one of those silly things that C++ does.

Also, welcome to the forums :)
Dec 22, 2008 at 3:09am
I believe you cannot split template functions up, they need to be included in the .cpp of every file that you want to use them in, because they are not normal functions. I would assume the same applies to a template class, (put it in all the .hpps) but I haven't checked.
Dec 22, 2008 at 4:26am
Dec 22, 2008 at 5:03pm
Indeed, I was declaring the class at a header file and trying to implement it on a source one. Putting everything together in the header file works fine! Thank you!

Well, I'll try to find a formal explanation about this behaviour ;P

Topic archived. No new replies allowed.