Allocating Template Objects Dynamically

Jul 12, 2009 at 3:51pm
closed account (1yR4jE8b)
Alright,

I need to write a data structure which may possibly need to contain very large objects, so dynamic memory allocation is a must. I've never needed to do dynamic allocation of objects that use templates so I am a little confused as to why this is not working. I've posted a very basic view of what I'm trying to do

container.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
25
26
27
28
29
30
31
32
33
34
35
36
37

#ifndef _Container_H
#define	_Container_H

template <class T>
class Container
{
private:
	typedef unsigned int size_type;

	T* data;
	size_type size;
	size_type capacity;
public:
	static const size_type DEFAULT_CAPACITY = 10;

	//constructors and destructor
	Container();
	Container(size_type initialCapacity);
	Container(const Container&);	//copy constructor
	~Container();

	//operators
	Container& operator=(const Container&);

	//accessor functions
	size_type getSize();

	//mutator functions
	bool resize();
	bool clear();
	bool insert(const T&);
	T& remove(size_type index);
	T& remove(const T&);
};

#endif	/* _Container_H */ 


container.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Container.h"

template <class T>
Bag<T>::Bag()
{
	data = new T[DEFAULT_CAPACITY];
	size = 0;
	capacity = DEFAULT_CAPACITY;
}

template <class T>
Bag<T>::Bag(size_type initialCapacity)
{
	data = new T[initialCapacity];
	size = 0;
	capacity = initialCapacity;
}

template <class T>
Bag<T>::~Bag()
{
	delete [] data;
}


Everything builds correctly, but when I try to test my constructors and create a test Container object, I get a "/home/darkestfright/Programming/C++/Container/main.cpp:15: undefined reference to `Container<double>::Container()'" error.

main.cpp:
1
2
3
4
5
6
7
8
#include <iostream>
#include "Container.h"

int main()
{
	Container<double> *testContainer = new Container<double>();
	return 0;
}


I am beside myself in confusion, and I've scoured Google but am unable to find the solution to my specific issue.
Jul 12, 2009 at 4:11pm
Your class named Container in the header, but Bag in the .cpp file.

also -- you generally can't separate template classes into cpp files like this because template classes are generated as needed by the .cpp files that use them. IE: main.cpp creates a Container<double>, but it cannot generate the code for Container<double> because it can't see the code for Container's member functions.

The general approach to this is to keep everything in the template class in the header.

Or, of course, you could just use std::vector. You're re-inventing the wheel here.
Jul 12, 2009 at 4:19pm
closed account (1yR4jE8b)
oh, the Bag thing in the .cpp file is a typo...

So when I'm writting Classes that use templates I can't seperate the declarations and the definitions, they need to be within the same file?
Jul 12, 2009 at 5:57pm
Generally / Simply: yes

Technically: No. There are ways to work around it. One of the easiest is to put the implementation in a separate header file and #include it in the normal header. IE:

1
2
3
4
5
6
7
8
9
// in container.h

template <class T>
class Container
{
 // ...
};

#include "container.hpp"  // or "container_imp.h" or whatever 


1
2
3
// in container.hpp / container_imp.h

// <put function bodies just as you would if this were a normal .cpp file 


Another alternative is to explicitly instantiate all the forms of the template you'll need so that they'll be externally linked... but that is generally a big pain in the butt.
Topic archived. No new replies allowed.