943,597 Members | Top Members by Rank

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

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11005
  • C++ RSS
Aug 30th, 2008
0

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

Expand Post »
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

Quote ...
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
entei is offline Offline
6 posts
since Aug 2008
Dec 23rd, 2009
0
Re: C++ Template success, methods in seperate .cpp file
some more methods are explained in http://www.codeproject.com/KB/cpp/Te...ementaion.aspx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
febilchacko is offline Offline
1 posts
since Dec 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to use system() command ?
Next Thread in C++ Forum Timeline: Sales problem c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC