Problem with template class

Hi Guys...First, sorry for my English, I'm not american...

I already know about templates class, but I can't split the declaration and the implementation into differente files. I'm trying a simples example:

File "include/myvector.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef _MY_VECTOR_H_
#define _MY_VECTOR_H_

#include <cstdlib>

template<class T>
class MyVector{
	protected:
		T *v;
		unsigned int m_size, max;
	public:
		MyVector();
		void realloc(unsigned int value);
		unsigned int size();
		T &operator[](unsigned int i);
		void push_back(T value);
		void clear();
		~MyVector();
};

#endif 


File "src/myvector.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
#include "../include/myvector.h"

template<class T>
class MyVector;

template<class T>
MyVector<T>::MyVector() : v(NULL), m_size(0), max(0){}

template<class T>
void MyVector<T>::realloc(unsigned int value){
	max = value;
	v = (T *)::realloc(v, sizeof(T) * value);
}

template<class T>
unsigned int MyVector<T>::size(){
	return m_size;
}

template<class T>
T &MyVector<T>::operator[](unsigned int i){
	return v[i];
}

template<class T>
void MyVector<T>::push_back(T value){
	if (m_size + 1 > max){
		realloc(max + 100);
	}
	v[m_size++] = value;
}

template<class T>
void MyVector<T>::clear(){
	if (v != NULL){
		free(v);
		v = NULL;
		m_size = max = 0;
	}
}

template<class T>
MyVector<T>::~MyVector(){
	clear();
}


File "main.cpp":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdio>
#include "include/myvector.h"

int main()
{
	MyVector<int> v;
	for (int i = 0; i < 200; i++)
		v.push_back(i);
	
	for (size_t i = 0; i < v.size(); i++)
		printf("%d\n", v[i]);
	
	return 0;
}


If I put the contents of "src/myvector.cpp" into the end of "include/myvector.h", then it works just fine. Does someone know why???

The compiler log is:
1
2
3
mingw32-g++.exe -Wall -fexceptions -g -O2 -Wall -ansi -pipe -Iinclude -c "C:\...\Templates\src\myvector.cpp" -o obj\Debug\src\myvector.o
mingw32-g++.exe -Wall -fexceptions -g -O2 -Wall -ansi -pipe -Iinclude -c "C:\...\Templates\main.cpp" -o obj\Debug\main.o
mingw32-g++.exe -o bin\Debug\Templates.exe obj\Debug\src\myvector.o obj\Debug\main.o


And the error I got is:
1
2
3
4
5
6
7
8
obj\Debug\main.o: In function `main':
C:/.../Templates/main.cpp:6: undefined reference to `MyVector::MyVector()'
C:/.../Templates/main.cpp:8: undefined reference to `MyVector::push_back(int)'
C:/.../Templates/main.cpp:10: undefined reference to `MyVector::size()'
C:/.../Templates/main.cpp:11: undefined reference to `MyVector::operator[](unsigned int)'
C:/.../Templates/main.cpp:10: undefined reference to `MyVector::size()'
C:/.../Templates/main.cpp:13: undefined reference to `MyVector::~MyVector()'
C:/.../Templates/main.cpp:13: undefined reference to `MyVector::~MyVector()'


So, if needed, I'm using Code::Blocks on Windows with Mingw32...

Thanks a lot...
Templates must be completely defined in a header. Move everything from the .cpp file into the header and it will work.
Juan Soulie wrote:
When the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appearance of myType by the type passed as the actual template parameter (int in this case) and then calls it. This process is automatically performed by the compiler and is invisible to the programmer.


Unless I've misunderstood something, that makes it rather hard to create an object file out of a file that has template definitions alone.

EDIT: Argh... just a few seconds late...

-Albatross
Last edited on
o.O

You guys are right!!! How I didn't think it?! If I generate a library, then the compiler can't know what type is needed to use...

But then, there is a question: The STL classes, like vector, list, etc. don't have libraries? All the source codes are in ".h" files?

Thank you guys...
I would think so...

-Albatross
humm... I'll do a research about that later...

For now, thanks a lot guys...

Matheus de Oliveira
But then, there is a question: The STL classes, like vector, list, etc. don't have libraries? All the source codes are in ".h" files?


That is correct1. Not everything in the standard library is entirely in headers, though.


1 At least in the GNU implementation.
Last edited on
Topic archived. No new replies allowed.