Templated Class Compile Error

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Templated Class Compile Error

 
0
  #1
Oct 22nd, 2008
I have three files (myClass.h, myClass.cpp, and main.cpp)

  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_ */

  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. }

  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

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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Templated Class Compile Error

 
1
  #2
Oct 22nd, 2008
I am not sure, but i think you should try including

iostream too.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Templated Class Compile Error

 
0
  #3
Oct 22nd, 2008
Originally Posted by Sky Diploma View Post
try including iostream too.
No change.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,495
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Templated Class Compile Error

 
0
  #4
Oct 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Templated Class Compile Error

 
1
  #5
Oct 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Templated Class Compile Error

 
0
  #6
Oct 22nd, 2008
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.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Templated Class Compile Error

 
0
  #7
Oct 22nd, 2008
SOLUTION:
TWO files: myClass.h main.cpp

  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_ */

  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 would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Templated Class Compile Error

 
1
  #8
Oct 22nd, 2008
Originally Posted by chococrack View Post
SOLUTION:
TWO files: myClass.h main.cpp

  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_ */

  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.

  1.  
  2. // file.h
  3. #ifndef MYHEADER_H
  4. #define MYHEADER_H
  5.  
  6. /*Declaractions*/
  7. #include "file.cpp"
  8.  
  9. #endif

  1.  
  2. // file.cpp
  3. #ifdef MYHEADER_H
  4.  
  5.  
  6. #endif

  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 519 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC