943,838 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 689
  • C++ RSS
Oct 22nd, 2008
0

Templated Class Compile Error

Expand Post »
I have three files (myClass.h, myClass.cpp, and main.cpp)

C++ Syntax (Toggle Plain Text)
  1. //
  2. // myClass.h
  3.  
  4. #ifndef MYCLASS_H_
  5. #define MYCLASS_H_
  6.  
  7. template <class element_type>
  8. class myClass
  9. {
  10. public:
  11. myClass();
  12.  
  13. ~myClass();
  14. };
  15.  
  16. #endif /* MYCLASS_H_ */

C++ Syntax (Toggle Plain Text)
  1. //
  2. // myClass.cpp
  3.  
  4. #include "myClass.h"
  5.  
  6. template <class element_type>
  7. myClass<element_type>::myClass()
  8. {
  9.  
  10. }
  11.  
  12.  
  13. template <class element_type>
  14. myClass<element_type>::~myClass()
  15. {
  16.  
  17. }

C++ Syntax (Toggle Plain Text)
  1. //
  2. // main.cpp
  3.  
  4.  
  5. #include "myClass.h"
  6.  
  7. int main()
  8. {
  9. myClass<int> classOBJ;
  10.  
  11.  
  12. return 0;
  13. }


I tried compiling this in VC++ Express, Dev-Cpp, and Eclipse

All give me roughly the same error. From eclipse

Quote 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.
Similar Threads
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 22nd, 2008
1

Re: Templated Class Compile Error

I am not sure, but i think you should try including

iostream too.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 22nd, 2008
0

Re: Templated Class Compile Error

try including iostream too.
No change.
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 22nd, 2008
0

Re: Templated Class Compile Error

Quote ...
i think you should try including
iostream too.
No need, theres no references to anything in iostream yet.
Last edited by William Hemsworth; Oct 22nd, 2008 at 12:18 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Oct 22nd, 2008
1

Re: Templated Class Compile Error

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 )...
Last edited by ArkM; Oct 22nd, 2008 at 12:28 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Oct 22nd, 2008
0

Re: Templated Class Compile Error

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.
Last edited by chococrack; Oct 22nd, 2008 at 12:47 pm.
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 22nd, 2008
0

Re: Templated Class Compile Error

SOLUTION:
TWO files: myClass.h main.cpp

C++ Syntax (Toggle Plain Text)
  1. //
  2. // myClass.h
  3.  
  4. #ifndef MYCLASS_H_
  5. #define MYCLASS_H_
  6.  
  7. template <class element_type>
  8. class myClass
  9. {
  10. public:
  11. myClass();
  12.  
  13. ~myClass();
  14. };
  15.  
  16. template <class element_type>
  17. myClass<element_type>::myClass()
  18. {
  19.  
  20. }
  21.  
  22.  
  23. template <class element_type>
  24. myClass<element_type>::~myClass()
  25. {
  26.  
  27. }
  28.  
  29. #endif /* MYCLASS_H_ */

C++ Syntax (Toggle Plain Text)
  1. //
  2. // main.cpp
  3.  
  4.  
  5. #include "myClass.h"
  6.  
  7. int main()
  8. {
  9. myClass<int> classOBJ;
  10.  
  11.  
  12. return 0;
  13. }
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 22nd, 2008
1

Re: Templated Class Compile Error

Click to Expand / Collapse  Quote originally posted by chococrack ...
SOLUTION:
TWO files: myClass.h main.cpp

C++ Syntax (Toggle Plain Text)
  1. //
  2. // myClass.h
  3.  
  4. #ifndef MYCLASS_H_
  5. #define MYCLASS_H_
  6.  
  7. template <class element_type>
  8. class myClass
  9. {
  10. public:
  11. myClass();
  12.  
  13. ~myClass();
  14. };
  15.  
  16. template <class element_type>
  17. myClass<element_type>::myClass()
  18. {
  19.  
  20. }
  21.  
  22.  
  23. template <class element_type>
  24. myClass<element_type>::~myClass()
  25. {
  26.  
  27. }
  28.  
  29. #endif /* MYCLASS_H_ */

C++ Syntax (Toggle Plain Text)
  1. //
  2. // main.cpp
  3.  
  4.  
  5. #include "myClass.h"
  6.  
  7. int main()
  8. {
  9. myClass<int> classOBJ;
  10.  
  11.  
  12. return 0;
  13. }
That's one way of doing it, but you could also use eager-inclusion.

c++ Syntax (Toggle Plain Text)
  1.  
  2. // file.h
  3. #ifndef MYHEADER_H
  4. #define MYHEADER_H
  5.  
  6. /*Declaractions*/
  7. #include "file.cpp"
  8.  
  9. #endif

c++ Syntax (Toggle Plain Text)
  1.  
  2. // file.cpp
  3. #ifdef MYHEADER_H
  4.  
  5.  
  6. #endif

c++ Syntax (Toggle Plain Text)
  1.  
  2. #include "file.h"
  3.  
  4. int main(){
  5.  
  6. return 0;
  7. }

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008

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: reg: c++ code for radix-2 FFT
Next Thread in C++ Forum Timeline: Help With Binary Search Program





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


Follow us on Twitter


© 2011 DaniWeb® LLC