Stupid problem about headers

Hello.
I have this code in 'grid.h':
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

template <typename T>
class Grid
{
	public:
		Grid(int inWidth = kDefaultWidth, int inHeight = kDefaultHeight);
		Grid(const Grid<T>& src);
		~Grid();
		Grid<T>& operator=(const Grid<T>& rhs);

		void setElementAt(int x, int y, const T& inElem);
		T& getElementAt(int x, int y);
		const T& getElementAt(int x, int y) const;
		int getHeight() const { return mHeight; }
		int getWidth() const { return mWidth; }
		static const int kDefaultWidth = 10;
		static const int kDefaultHeight = 10;

	protected:
		int mWidth, mHeight;
		T** mCells;
		void copyFrom(const Grid<T>& src);
		void freeMemory();
};


the following code in 'grid.cpp':
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "grid.h"

template <typename T>
Grid<T>::Grid(int inWidth, int inHeight) : mWidth(inWidth), mHeight(inHeight)
{
	mCells = new T* [mWidth];
	for (int i = 0; i < mWidth; i++) {
		mCells[i] = new T[mHeight];
	}
}

template <typename T>
const int Grid<T>::kDefaultWidth;

template <typename T>
const int Grid<T>::kDefaultHeight;

template <typename T>
Grid<T>::Grid(const Grid<T>& src)
{
	copyFrom(src);
}

template <typename T>
Grid<T>::~Grid()
{
	freeMemory();
}

template <typename T>
Grid<T>& Grid<T>::operator=(const Grid<T>& rhs)
{
	if (this == &rhs) {
		return (*this);
	}

	freeMemory();
	copyFrom(rhs);

	return (*this);
}

template <typename T>
void Grid<T>::setElementAt(int x, int y, const T& element)
{
	mCells[x][y] = element;
}

template <typename T>
T& Grid<T>::getElementAt(int x, int y)
{
	return (mCells[x][y]);
}

template <typename T>
const T& Grid<T>::getElementAt(int x, int y) const
{
	return (mCells[x][y]);
}

template <typename T>
void Grid<T>::copyFrom(const Grid<T>& src)
{
	mWidth = src.mWidth;
	mHeight = src.mHeight;

	mCells = new T* [mWidth];
	for (int i = 0; i < mWidth; i++) {
		mCells[i] = new T[mHeight];
	}

	for (int i = 0; i < mWidth; i++) {
		for(int j = 0; j < mHeight; j++) {
			mCells[i][j] = src.mCells[i][j];
		}
	}

}

template <typename T>
void Grid<T>::freeMemory()
{
	for(int i = 0; i < mWidth; i++)
	{
		delete [] mCells[i];
	}
}


and this in 'main.cpp':
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

#include "grid.h"

int main(int argc, char** argv)
{
	Grid<int> myIntGrid(10,10);
	myIntGrid.setElementAt(0, 0, 10);
	int x = myIntGrid.getElementAt(0, 0);

	return (0);
}


OK. Here's the structure:
Grid.h is included by Grid.cpp (its definition) and Main.cpp (a 'test' method).
But when I compile, I receive the following list of errors:
1
2
3
4
5
6
7
8
9
C:\Users\...\Projects\C-C++\Sample\main.cpp||In function `int main(int, char**)':|
C:\Users\...\Projects\C-C++\Sample\main.cpp|9|warning: unused variable 'x'|
obj\Debug\main.o||In function `main':|
C:\Users\...\Projects\C-C++\Sample\main.cpp|7|undefined reference to `Grid<int>::Grid(int, int)'|
C:\Users\...\Projects\C-C++\Sample\main.cpp|8|undefined reference to `Grid<int>::setElementAt(int, int, int const&)'|
C:\Users\...\Projects\C-C++\Sample\main.cpp|9|undefined reference to `Grid<int>::getElementAt(int, int)'|
C:\Users\...\Projects\C-C++\Sample\main.cpp|11|undefined reference to `Grid<int>::~Grid()'|
C:\Users\...\Projects\C-C++\Sample\main.cpp|11|undefined reference to `Grid<int>::~Grid()'|
||=== Build finished: 5 errors, 1 warnings ===| 

But if I append the content of 'grid.cpp' (the implementation) to 'grid.h' (the declaration), it compiles without error.
And the other strange thing is that when I include the file grid.cpp in the main.cpp, the compilation is successfull!
Please help me.
Last edited on
With templates you have to put the function implementations in the same file ( the header ) of the declarations
thank you.
Something, even if you hadn't used templates, you never actually included grid.cpp.
You aren't supposed to include cpp files
Topic archived. No new replies allowed.