• Advertisement

Archived

This topic is now archived and is closed to further replies.

c++ templates trouble

This topic is 5101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently making a quick class for use as a 2d version of the stl vector class. (i've named it matrix) Header:
#include <vector>

template <class T>
class Matrix {
public:
	Matrix(); //default ctor

	Matrix(int xDimension, int yDimension); //config ctor

	~Matrix(); //dtor

	void in(int x, int y, T data); //store in matrix

	T out(int x, int y);  //retrieve value from matrix

private:
	std::vector<T> levelmap; //store matrix

	int xSize;
};//CLASS Matrix

 
Definitions:
#include "matrix.h"


template <class T>
Matrix<T>::Matrix() {
	levelmap.resize(100,NULL);
	xSize = 10;
}



template <class T>
Matrix<T>::Matrix(int xDimension, int yDimension) {
	levelmap.resize((xDimension*yDimension),NULL);
	xSize = xDimension;
}



template <class T>
Matrix<T>::~Matrix() { }



template <class T>
void Matrix<T>::in(int x, int y, T data) {
	//if((xSize*y)+x > levelmap.capacity())

	levelmap[((xSize * y) + x)] = data;
}



template <class T>
T Matrix<T>::out(int x, int y) {
	//add bounds check

	return levelmap.at((xSize * y)+x);
} 
Usage:
Matrix<int> mapTex; //Construct for holding level details

Matrix<Sprite> mapSprite; //Construct for holding sprites 
(Note: The type "Sprite" is defined elsewhere and works) The problem I have is that it produces the errors: architect.obj : error LNK2001: unresolved external symbol "public: __thiscall Matrix::Matrix(void)" (??0?$Matrix@H@@QAE@XZ) architect.obj : error LNK2001: unresolved external symbol "public: __thiscall Matrix::~Matrix(void)" (??1?$Matrix@H@@QAE@XZ) architect.obj : error LNK2001: unresolved external symbol "public: __thiscall Matrix::Matrix(void)" (??0?$Matrix@VSprite@@@@QAE@XZ) architect.obj : error LNK2001: unresolved external symbol "public: __thiscall Matrix::~Matrix(void)" (??1?$Matrix@VSprite@@@@QAE@XZ) This to me suggested that it couldn't find the ctors/dtor, so I commented out the ctors and dtor and it compiled. I know it's not namespace pollution as I've tried renaming the class and have the same trouble. Any suggestions? [edited by - ShAdOwStRyKeR on February 5, 2004 5:16:46 PM]

Share this post


Link to post
Share on other sites
Advertisement
Template member function definitions should be included in the header with the template class declaration. Separate compilation of template functions is in extremely icky state of affairs right now.

(Translation: move the stuff from the matrix cpp file into the header and all should be good.)

Share this post


Link to post
Share on other sites
SiCrane got it. Another way is to #include "Matrix.cpp" under your class definition in the header file. Both ways are equally practicle.

--Viktor

Share this post


Link to post
Share on other sites
Thanks a lot for the replies. This was the first time I''ve actually needed to use a template class. It''s now working thanks to your help. :-)

Share this post


Link to post
Share on other sites

  • Advertisement