undefined reference (again)

everything runs properly if i include "vector2.cpp" in my test script

undefined reference errors occur if i include vector2.h

i have no idea.
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
#ifndef VECTOR2_H
#define VECTOR2_H


//simple vector
template <class T>
class vector{

   public:
	vector(unsigned int numOfElements);
	vector(unsigned int numOfElements, T & initValue);
	vector(const vector & source);
	virtual ~vector();

	//access
	T & operator [] (unsigned int index) const;

	//return length
	unsigned int length () const;

	//dynamically change size 
	unsigned int setSize (unsigned int numberOfElements);
	unsigned int setSize (unsigned int numberOfElements, T& initValue);

   protected:
	//data area
	T * data;
	unsigned int size;
};

#endif 


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
88
89
90
91
92
93
94
95
96
97
98
#include "vector2.h"
#include "assert.h"

template <class T>
vector <T> :: vector (unsigned int numOfElements){
	data = new T [numOfElements];
	assert (data!= 0 );
	size = numOfElements;
}

template <class T>
vector <T> :: vector (unsigned int numOfElements, T & initValue) {
	size = numOfElements;
	data = new T [size];
	assert (data!= 0);

	for (int i=0; i< size; i++)
		data[i] = initValue;

}

template <class T>
vector <T> :: vector (const vector <T> & source) {
	size = source.size;
	data = new T [size];
	assert ( data != 0);
	
	for (int i=0; i< size; i++)
		data[i] = source.data[i];
}


template <class T>
vector <T> :: ~vector () {
	delete [] data;
	data = 0;
	size = 0;
}

//the reference is so that it can be put on the left side
template <class T>
T & vector <T> :: operator [] (unsigned int index) const {
	assert (index < size);
	return data[index];
}

template <class T>
unsigned int vector <T> :: length () const {
	return size;
}

template <class T>
unsigned int vector <T> :: setSize (unsigned int numberOfElements) {
//return new size

	T * newData = new T [numberOfElements];
	assert (newData != 0);
	if (numberOfElements < size){
		for (int i = 0; i<numberOfElements; i++)
			newData [i] = data[i];
	}
	else {
		for (int i = 0; i<size; i++)
			newData [i] = data[i];
	}

	delete [] data;
	data = newData;
	size = numberOfElements;
	return size;
}
 

template <class T>
unsigned int vector <T> :: setSize (unsigned int numberOfElements, T & initValue) {
//return new size

	T * newData = new T [numberOfElements];
	assert (newData != 0);
	if (numberOfElements < size){
		for (int i = 0; i<numberOfElements; i++)
			newData [i] = data[i];
	}
	else {
		for (int i = 0; i<size; i++)
			newData [i] = data[i];
		for (int i = size; i<numberOfElements; i++)
			newData [i] = initValue;
	}

	delete [] data;
	data = newData;
	size = numberOfElements;
	return size;
}





1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "vector2.h"

int main () {
	int init = -1;

	vector<int> num (10, init);
	for (int i = 0; i< 10; i++)
		num[i] = num[i] + 1;

	for (int i=0; i< 10; i ++)
		std::cout << " i[" << i << "] is " << num[i] << std::endl;
}




Thanks for any help.


















Last edited on
As Far as I Know, you can't seperate prototype and implementation of a template class as a header file and a source file.
Last edited on
really? I see. This makes some sense. Is there another way to hide the implementation then? or do i put both in one file?
Dufresne is right. With templates the implementation has to go on the header unless your compiler supports the "export" keyword. (Which most don't)
Is there another way to hide the implementation then? or do i put both in one file?

There is :
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
somewhere on this page you will find the solution.

Topic archived. No new replies allowed.