943,660 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 862
  • C++ RSS
Sep 6th, 2009
0

compiler error code

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyosuke0 is offline Offline
20 posts
since Sep 2008
Sep 6th, 2009
1

Re: compiler error code

You forgot to include the contents of: "nodeType.h"
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 6th, 2009
0

Re: compiler error code

Click to Expand / Collapse  Quote originally posted by DdoubleD ...
You forgot to include the contents of: "nodeType.h"

sorry about that

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyosuke0 is offline Offline
20 posts
since Sep 2008
Sep 6th, 2009
0

Re: compiler error code

What is the definition of Storeable? You declare types, but I don't see the definition.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 6th, 2009
0

Re: compiler error code

Click to Expand / Collapse  Quote originally posted by DdoubleD ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyosuke0 is offline Offline
20 posts
since Sep 2008
Sep 6th, 2009
0

Re: compiler error code

Click to Expand / Collapse  Quote originally posted by kyosuke0 ...
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.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 6th, 2009
0

Re: compiler error code

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...
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 6th, 2009
0

Re: compiler error code

Click to Expand / Collapse  Quote originally posted by DdoubleD ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyosuke0 is offline Offline
20 posts
since Sep 2008
Sep 6th, 2009
0

Re: compiler error code

Anytime. Cheers!
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Problem with colors in console
Next Thread in C++ Forum Timeline: Help me improve my algo/code





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


Follow us on Twitter


© 2011 DaniWeb® LLC