RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 280 | Replies: 7 | Thread Tools  Display Modes
Reply
Join Date: Oct 2008
Location: Louisiana
Posts: 116
Reputation: chococrack is on a distinguished road 
Rep Power: 1
Solved Threads: 13
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Templated Class Compile Error

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

//
// 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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2008
Posts: 246
Reputation: Sky Diploma is on a distinguished road 
Rep Power: 1
Solved Threads: 34
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Posting Whiz in Training

Re: Templated Class Compile Error

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

iostream too.
User, n. The word computer professionals use when they mean "idiot." ~Dave Barry
Reply With Quote  
Join Date: Oct 2008
Location: Louisiana
Posts: 116
Reputation: chococrack is on a distinguished road 
Rep Power: 1
Solved Threads: 13
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Templated Class Compile Error

  #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  
Join Date: Mar 2008
Location: UK - Lymm
Posts: 599
Reputation: williamhemsworth is just really nice williamhemsworth is just really nice williamhemsworth is just really nice williamhemsworth is just really nice williamhemsworth is just really nice 
Rep Power: 6
Solved Threads: 56
williamhemsworth's Avatar
williamhemsworth williamhemsworth is offline Offline
Posting Pro

Re: Templated Class Compile Error

  #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 williamhemsworth : Oct 22nd, 2008 at 12:18 pm.
Reply With Quote  
Join Date: Jul 2008
Posts: 1,197
Reputation: ArkM is a glorious beacon of light ArkM is a glorious beacon of light ArkM is a glorious beacon of light ArkM is a glorious beacon of light ArkM is a glorious beacon of light 
Rep Power: 7
Solved Threads: 185
ArkM's Avatar
ArkM ArkM is offline Offline
Veteran Poster

Re: Templated Class Compile Error

  #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  
Join Date: Oct 2008
Location: Louisiana
Posts: 116
Reputation: chococrack is on a distinguished road 
Rep Power: 1
Solved Threads: 13
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Templated Class Compile Error

  #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  
Join Date: Oct 2008
Location: Louisiana
Posts: 116
Reputation: chococrack is on a distinguished road 
Rep Power: 1
Solved Threads: 13
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Templated Class Compile Error

  #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  
Join Date: Jun 2008
Location: WA, USA
Posts: 940
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 
Rep Power: 5
Solved Threads: 100
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Templated Class Compile Error

  #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
  10.  

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

  1.  
  2. #include "file.h"
  3.  
  4. int main(){
  5.  
  6. return 0;
  7. }
  8.  

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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:22 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC