| | |
Templated Class Compile Error
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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?
C++ Syntax (Toggle Plain Text)
// // myClass.h #ifndef MYCLASS_H_ #define MYCLASS_H_ template <class element_type> class myClass { public: myClass(); ~myClass(); }; #endif /* MYCLASS_H_ */
C++ Syntax (Toggle Plain Text)
// // myClass.cpp #include "myClass.h" template <class element_type> myClass<element_type>::myClass() { } template <class element_type> myClass<element_type>::~myClass() { }
C++ Syntax (Toggle Plain Text)
// // 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
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; }
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
- 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
Views: 519 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






