1.11M Members

C++ Template success, methods in seperate .cpp file

 
0
 

I'm fairly new to C++, I've only been learning it for 4 months now and I've been having problems with a template class file that i was creating for my project i kept getting the error

main.obj : error LNK2019: unresolved external symbol "public: __thiscall Intervals<float>::Intervals<float>(float,float)" (??0?$Intervals@M@@QAE@MM@Z) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Intervals<int>::Intervals<int>(int,int)" (??0?$Intervals@H@@QAE@HH@Z) referenced in function _main
C:\Documents and Settings\Smoothplayer\My Documents\Visual Studio 2008\Projects\BOTS\Debug\BOT.exe : fatal error LNK1120: 2 unresolved externals

the code for my template files are
Header file

//Intervals.h

template <typename T>
class Intervals
{
public:
	Intervals();
	Intervals(T start, T end);
	T midpoint();
	/....

private:
	T mStart;
	T mEnd;
};

Implemetation of the template's methods file

//Intervals.cpp
#include "Intervals.h"
template <typename T>
Intervals<T>::Intervals()
{
	mStart  = T();
	mEnd	= T();
}

template <typename T>
Intervals<T>::Intervals(T start, T end)
{
	mStart	= start;
	mEnd	= end;
}

template <typename T>
T Intervals<T>::midpoint()
{
	return T((mStart + mEnd) *0.5);
}

Main.cpp

//main.cpp

#include "Intervals.h"
#include <iostream>
using namespace std;

int main()
{
	Intervals<int>		I(0,3);
	Intervals<float>	J(2.0f,10.0f);

//...

	return 0;
}

When i put copy the method for the template to the header file theres no errors, but i was thinking an hoping there would be a way to have the methods on a different .cpp file like i showed above. I've tried alot of forums and searched for alot of them, but i had no luck, and alot of their suggestions, for some reason didn't work.

For those who want to understand more about how templates work and why you get this error the link bellow explains it pretty well even a simple starter programmer like me understands. the link is http://www.ecs.fullerton.edu/~sowell/cs331/TemplateClasses.html

the suggestions that they made are worth trying out if your getting error like the ones i got, unfortunately they didnt work for me as i wanted the template methods to be in a different file partly because it is recommeded for classes, and i wanted it that way.

Anyway I thought hard about this and SUCCESS I've found a way to have the template class in two files and for it to work. The only problem is that it works for me in the situation above, but I'm not sure if it is the correct way that it should be done, so i would like some feedback if posible. thanks.

anyway the way i found to do this is by using include shields #ifndef/endif. the way to do this is to enclose the header file cotaining the template class decleration in the include shield like i've shown below, and also to include the class method file inside the shield, at the end of the class declaration.

//Intervals.h

#ifndef INTERVALS_H
#define INTERVALS_H

template <typename T>
class Intervals
{
public:
	Intervals();
	Intervals(T start, T end);
	T midpoint();
	/....

private:
	T mStart;
	T mEnd;
};

#include <Intervals.cpp> // Includes the template methods file
#endif //INTERVALS_H

Then in the class method file, enclose the template class methods with #ifdef/endif, note you dont need to include the header file for the class in this file. see code below.

//Intervals.cpp

#ifdef INTERVALS_H

template <typename T>
Intervals<T>::Intervals()
{
	mStart  = T();
	mEnd	= T();
}

template <typename T>
Intervals<T>::Intervals(T start, T end)
{
	mStart	= start;
	mEnd	= end;
}

template <typename T>
T Intervals<T>::midpoint()
{
	return T((mStart + mEnd) *0.5);
}

#endif

It took some time for me to figure this out, but i welcome any correction in code or explaination, and comments on whether it works in other situations, bear in mind that I've only tested it for the class templates for my current project.

 
0
 

It works for my similar problem, thanks

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article