Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Templates and multiple source files

Status
Not open for further replies.

chpicker

Programmer
Apr 10, 2001
1,316
This isn't specific to VC++ but appears to be a general C++ issue. I'm trying to learn how to use templates properly. I've run into a strange problem, though.

Take the following program:
Code:
#include <iostream.h>

class test {
public:
	int theNum;
};

template <class T>
class mine {
public:
	mine();
	~mine();
	
	T* thePointer;

	T* GetPointer();
};

template <class T>
mine<T>::mine():thePointer(NULL) {}

template <class T>
mine<T>::~mine() {if (thePointer) delete thePointer;}

template <class T>
T* mine<T>::GetPointer() {return thePointer;}

void main() {
	test MyTest;
	MyTest.theNum=5;

	mine<test> excalibur;
	excalibur.thePointer=new test;

	(excalibur.GetPointer())->theNum=6;

	cout << "MyTest.theNum is " << MyTest.theNum << endl;
	cout << "excalibur's num is " << excalibur.thePointer->theNum << endl;
}
This program compiles and links just fine. However, what I want to do is split the program into 3 files: one header and source file for the classes and one source file for the main function. This division would look like this.
Code:
// class.h
#ifndef __CLASS_H__
#define __CLASS_H__

class test {
public:
	int theNum;
};

template <class T>
class mine {
public:
	mine();
	~mine();
	
	T* thePointer;

	T* GetPointer();
};

#endif
Code:
// class.cpp
#include "class.h"

template <class T>
mine<T>::mine():thePointer(NULL) {}

template <class T>
mine<T>::~mine() {if (thePointer) delete thePointer;}

template <class T>
T* mine<T>::GetPointer() {return thePointer;}
Code:
// main.cpp
#include <iostream.h>
#include "class.h"

void main() {
	test MyTest;
	MyTest.theNum=5;

	mine<test> excalibur;
	excalibur.thePointer=new test;

	(excalibur.GetPointer())->theNum=6;

	cout << "MyTest.theNum is " << MyTest.theNum << endl;
	cout << "excalibur's num is " << excalibur.thePointer->theNum << endl;
}
This program also compiles just fine. However, when it links, I get unresolved externals on all of the template functions that get used, including the constructor and destructor.

I rewrote the class.cpp and class.h files, removing the template indicators and changing any "T" references to "test". It compiles and links just fine.

What am I missing? What do I need to do differently when I use templates if I want to be able to organize my source code into multiple files?

Ian
 
This is the big problem with templates, and in my opinion, there are currently no good solutions. Here are two ways to get around this

1. Define all of your methods inside the class where they are declared.
2. At the top of the cpp, explicitly create the classes for each type you will use in your project. The syntax for this is "template mine<int>; template mine<double>;", where you'll be using the class with template types of int and double. You'll get warnings but it should compile.
 
Thanks for the input, guys. It makes me feel a bit better that the problem wasn't in my code!

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top