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

Please support our C++ advertiser: Intel Parallel Studio Home
View Poll Results: Was this helpfull to you
Yes its helpful - and it worked 0 0%
No - I still get errors 0 0%
Doesn't work in my situation 0 0%
-this is full of errors 0 0%
Multiple Choice Poll. Voters: 0. You may not vote on this poll

Reply

Join Date: Aug 2008
Posts: 6
Reputation: entei is an unknown quantity at this point 
Solved Threads: 1
entei entei is offline Offline
Newbie Poster

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

 
0
  #1
Aug 30th, 2008
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
  1. //Intervals.cpp
  2. #include "Intervals.h"
  3. template <typename T>
  4. Intervals<T>::Intervals()
  5. {
  6. mStart = T();
  7. mEnd = T();
  8. }
  9.  
  10. template <typename T>
  11. Intervals<T>::Intervals(T start, T end)
  12. {
  13. mStart = start;
  14. mEnd = end;
  15. }
  16.  
  17. template <typename T>
  18. T Intervals<T>::midpoint()
  19. {
  20. return T((mStart + mEnd) *0.5);
  21. }
Main.cpp
  1. //main.cpp
  2.  
  3. #include "Intervals.h"
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. Intervals<int> I(0,3);
  10. Intervals<float> J(2.0f,10.0f);
  11.  
  12. //...
  13.  
  14. return 0;
  15. }

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...teClasses.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.

  1. //Intervals.cpp
  2.  
  3. #ifdef INTERVALS_H
  4.  
  5. template <typename T>
  6. Intervals<T>::Intervals()
  7. {
  8. mStart = T();
  9. mEnd = T();
  10. }
  11.  
  12. template <typename T>
  13. Intervals<T>::Intervals(T start, T end)
  14. {
  15. mStart = start;
  16. mEnd = end;
  17. }
  18.  
  19. template <typename T>
  20. T Intervals<T>::midpoint()
  21. {
  22. return T((mStart + mEnd) *0.5);
  23. }
  24.  
  25. #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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 3815 | Replies: 0
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC