| | |
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 | |||
![]() |
•
•
Join Date: Aug 2008
Posts: 6
Reputation:
Solved Threads: 1
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
the code for my template files are
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
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.
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.
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.
•
•
•
•
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
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)
//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
C++ Syntax (Toggle Plain Text)
//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...teClasses.htmlthe 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_HThen 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)
//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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need Help with this code
- Next Thread: Generate 10 random numbers (1-100) using bubblesort
Views: 3815 | Replies: 0
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





