Trouble declaring new object

I have a pretty simple class
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
  class Car
{
public:
	/*
	 * default constructor
	 */
	Car();

	/*
	 * constructor
	 */
	Car(int nr, char *model, char *cat);

	/*
	 * copy constructor
	 */
	Car(const Car &ot);

	int getNr();

private:
	int nr;
	char *model;
	char *cat;
};


And a little more complex one, that's essentially a self-made array.

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
template<typename Element>
class DynamicArray
{
private:
	Element *elements;
	int capacity;
	int size;
public:
	/*
	 * default constructor
	 */
	DynamicArray();

	/*
	 * copy constructor
	 */
	DynamicArray(const DynamicArray &ot);

	/*
	 * assignment operator
	 */
	DynamicArray& operator=(const DynamicArray& ot);

	/*
	 * destructor
	 */
	~DynamicArray();

	/*
	 * adds new element
	 * params:
	 * 		e - element
	 */
	void addElement(Element e);

	/*
	 * deletes element from position
	 * params
	 * 		poz - 0 <= poz < size
	 */
	void deleteElement(int poz);

	/*
	 * returns element from position
	 * params:
	 * 		poz - 0 <= poz < size
	 */
	Element getElement(int poz);

	/*
	 * inserts element
	 * params
	 * 		e - element
	 * 		poz - 0 <= poz < size
	 */
	void set(Element el, int poz);

	/*
	 * 
	 */
	int getSize();

	/*
	 * deletes all elements from the array, but does not destroy the array
	 */
	void clear();

	/*
	 *
	 */
	void ensureCapacity(int nrElements);
};


I'm using template because I plan on using this for more than one data type. My problem is when I try to declare a new DynamicArray:

 
	DynamicArray<Car> arr;

I get 2 errors:

Undefined reference to DynamicArray<Car>::~DynamicArray()
Undefined reference to DynamicArray<Car>::DynamicArray()


My default constructor:
1
2
3
4
5
6
7
template<typename Element>
DynamicArray<Element>::DynamicArray()
{
	this->capacity = 1;
	this->elements = new Element[capacity];
	this->size = 0;
}


I think I make some wrong connections when reading about how to do this so point me into the right direction, please.
For template classes, you need to implement the classes in-place: i.e. the functions must all be implemented in the header file with the declaration of the source code.
This means that I'll have no .cpp for the given header? At least it's not something hard to do. Thanks :)
But what's the reason behind this?

Also, just out of curiosity, for my Car class what is better, to use string objects or to keep the current char pointers? Or this is more context-dependent?
Last edited on
Generally, unless you really need efficiency, and can prove that efficiency is an issue, using strings is better, as you are less likely to make mistakes, and its just easier to deal with - its also the 'C++' way of doing things.

As for why you need to do the moving of the templates, is due to the way that templates are managed by compilers. A template isn't actually a function, as the name implies, its a template of a function - i.e. it needs to be instantiated for each time you call it. So, when you call a template function, the compiler will see (for example) you are calling it with T as int, and if you don't already have one it will create a function that is your templated function, but replacing T with int and then it might inline it into your code. However, if it was implemented in a different source file, the compiler doesn't know how to implement the function... I hope this makes sense.
Topic archived. No new replies allowed.