Please support our C++ advertiser: Programming Forums
![]() |
I have three files (myClass.h, myClass.cpp, and main.cpp)
I tried compiling this in VC++ Express, Dev-Cpp, and Eclipse
All give me roughly the same error. From eclipse
This leads me to believe that I'm coding something wrong, but I just can't track it down for some reason. What's the problem?
//
// myClass.h
#ifndef MYCLASS_H_
#define MYCLASS_H_
template <class element_type>
class myClass
{
public:
myClass();
~myClass();
};
#endif /* MYCLASS_H_ *///
// myClass.cpp
#include "myClass.h"
template <class element_type>
myClass<element_type>::myClass()
{
}
template <class element_type>
myClass<element_type>::~myClass()
{
}//
// main.cpp
#include "myClass.h"
int main()
{
myClass<int> classOBJ;
return 0;
}I tried compiling this in VC++ Express, Dev-Cpp, and Eclipse
All give me roughly the same error. From eclipse
•
•
•
•
Originally Posted by errors
undefined reference to `myClass<int>::~myClass()' main.cpp heapProj 15 C/C++ Problem
undefined reference to `myClass<int>::myClass()' main.cpp heapProj 12 C/C++ Problem
This leads me to believe that I'm coding something wrong, but I just can't track it down for some reason. What's the problem?
Last edited by chococrack : Oct 22nd, 2008 at 12:09 pm.
I would love to change the world, but they won't give me the source code
Alas, it's impossible now to separate template class declaration and implementation. Why? Have a look at
http://www.parashift.com/c++-faq-lit...html#faq-35.12
So place all your cpp file contents into template definition header file (then wait for a new compilers generation
)...
http://www.parashift.com/c++-faq-lit...html#faq-35.12
So place all your cpp file contents into template definition header file (then wait for a new compilers generation
)... Last edited by ArkM : Oct 22nd, 2008 at 12:28 pm.
That's got to be it, because I do know that if I stack them all together in one file, everything runs without a problem.
The only compiler I know of that actually allows this separation so far (in my studies) is the Sun Studio 12 "CC" compiler
That link is golden.
The only compiler I know of that actually allows this separation so far (in my studies) is the Sun Studio 12 "CC" compiler
That link is golden.
Last edited by chococrack : Oct 22nd, 2008 at 12:47 pm.
I would love to change the world, but they won't give me the source code
SOLUTION:
TWO files: myClass.h main.cpp
TWO files: myClass.h main.cpp
C++ Syntax (Toggle Plain Text)
// // myClass.h #ifndef MYCLASS_H_ #define MYCLASS_H_ template <class element_type> class myClass { public: myClass(); ~myClass(); }; template <class element_type> myClass<element_type>::myClass() { } template <class element_type> myClass<element_type>::~myClass() { } #endif /* MYCLASS_H_ */
C++ Syntax (Toggle Plain Text)
// // main.cpp #include "myClass.h" int main() { myClass<int> classOBJ; return 0; }
I would love to change the world, but they won't give me the source code
•
•
•
•
SOLUTION:
TWO files: myClass.h main.cpp
C++ Syntax (Toggle Plain Text)
// // myClass.h #ifndef MYCLASS_H_ #define MYCLASS_H_ template <class element_type> class myClass { public: myClass(); ~myClass(); }; template <class element_type> myClass<element_type>::myClass() { } template <class element_type> myClass<element_type>::~myClass() { } #endif /* MYCLASS_H_ */
C++ Syntax (Toggle Plain Text)
// // main.cpp #include "myClass.h" int main() { myClass<int> classOBJ; return 0; }
That's one way of doing it, but you could also use eager-inclusion.
c++ Syntax (Toggle Plain Text)
// file.h #ifndef MYHEADER_H #define MYHEADER_H /*Declaractions*/ #include "file.cpp" #endif
c++ Syntax (Toggle Plain Text)
// file.cpp #ifdef MYHEADER_H #endif
c++ Syntax (Toggle Plain Text)
#include "file.h" int main(){ return 0; }
And also instead of eager inclusion you can simply add the .cpp file later in the driver file, and include the header file in the driver file.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Class Template Problem - Constructor Issues - Please help (C++)
- missing storage-class or type specifiers error (C++)
Other Threads in the C++ Forum
- Previous Thread: reg: c++ code for radix-2 FFT
- Next Thread: Help With Binary Search Program
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode