compiler error code

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

compiler error code

 
0
  #1
Sep 6th, 2009
Iv always had trouble with templates and I always get the same error message "Error: Templates can only declare classes or functions."

program is a template linked list class and I'm using the sun studio 12 C++ compiler

linkedList.h file:
  1. #include "nodeType.h"
  2.  
  3. using namespace std;
  4.  
  5. template <class Storeable>
  6. class LinkedList
  7. {
  8. public:
  9.  
  10. LinkedList();
  11. //default constructor
  12.  
  13. bool isEmpty();
  14. //Postcondition: should return true if list is empty,
  15. //false if the list is not empty
  16. .
  17. .
  18. .
  19. ~LinkedList();
  20. //destructor
  21. private:
  22.  
  23. nodeType<Storeable> *first;
  24. //pointer to the first node in the list
  25.  
  26. nodeType<Storeable> *last;
  27. //pointer to the last node in the list
  28. };

linkedList.cpp file:
  1. #include "linkedList.h"
  2.  
  3. using namespace std;
  4.  
  5. template <class Storeable>
  6. linkedList<Storeable>::linkedList()
  7. {
  8. first = NULL;
  9. last = NULL;
  10. }
  11.  
  12. template <class Storeable>
  13. linkedList<Storeable>::isEmpty()
  14. {
  15. if(first == NULL)
  16. return true;
  17. else
  18. return false;
  19. }
  20. .
  21. .
  22. .

I'm basically get that "Error: Templates can only declare classes or functions." every line where "template<class Storeable>" is in the linkedList.cpp file. I'v been searching google and couldn't find anything. any help is appreciated
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 917
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: compiler error code

 
1
  #2
Sep 6th, 2009
You forgot to include the contents of: "nodeType.h"
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

Re: compiler error code

 
0
  #3
Sep 6th, 2009
Originally Posted by DdoubleD View Post
You forgot to include the contents of: "nodeType.h"

sorry about that

  1. template <class Storeable>
  2. struct nodeType
  3. {
  4. Storeable info;
  5. nodeType<Storeable> *link;
  6. };
Last edited by kyosuke0; Sep 6th, 2009 at 6:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 917
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: compiler error code

 
0
  #4
Sep 6th, 2009
What is the definition of Storeable? You declare types, but I don't see the definition.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

Re: compiler error code

 
0
  #5
Sep 6th, 2009
Originally Posted by DdoubleD View Post
What is the definition of Storeable? You declare types, but I don't see the definition.

so i should replace "Storeable" with "Type" ? My teacher didn't say much about templates, i was under the impression that you can put whatever word you wanted like a variable. The book im looking at uses "Type" so thats where i got that from.

am i missing something else painfully obvious?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 917
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: compiler error code

 
0
  #6
Sep 6th, 2009
Originally Posted by kyosuke0 View Post
so i should replace "Storeable" with "Type" ? My teacher didn't say much about templates, i was under the impression that you can put whatever word you wanted like a variable. The book im looking at uses "Type" so thats where i got that from.

am i missing something else painfully obvious?
No, disregard my previous, but brief rant--LOL. These types of errors can be so elusive... I haven't seen the "obvious" here either yet.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 917
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: compiler error code

 
0
  #7
Sep 6th, 2009
OK, found something obvious: You have defined "linkedList" name for definition (in .cpp), but "LinkedList" name for declaration (in .h). Fix that and see what you get...

Also, you left off the "bool" keyword in definition of isEmpty in .cpp. Change to: template <class Storeable> bool LinkedList<Storeable>::isEmpty()
Last edited by DdoubleD; Sep 6th, 2009 at 8:01 pm. Reason: and another thing...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

Re: compiler error code

 
0
  #8
Sep 6th, 2009
Originally Posted by DdoubleD View Post
OK, found something obvious: You have defined "linkedList" name for definition (in .cpp), but "LinkedList" name for declaration (in .h). Fix that and see what you get...

Also, you left off the "bool" keyword in definition of isEmpty in .cpp. Change to: template <class Storeable> bool LinkedList<Storeable>::isEmpty()
ah I seemed to have done that for all the member functions and not just isEmpty, fixing that seems to have fixed the template error message but uncovered a makefile error. If i have questiongs i know where to go.

thank you very much DdoubleD
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 917
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 147
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: compiler error code

 
0
  #9
Sep 6th, 2009
Anytime. Cheers!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC