| | |
compiler error code
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
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:
linkedList.cpp file:
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
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)
#include "nodeType.h" using namespace std; template <class Storeable> class LinkedList { public: LinkedList(); //default constructor bool isEmpty(); //Postcondition: should return true if list is empty, //false if the list is not empty . . . ~LinkedList(); //destructor private: nodeType<Storeable> *first; //pointer to the first node in the list nodeType<Storeable> *last; //pointer to the last node in the list };
linkedList.cpp file:
C++ Syntax (Toggle Plain Text)
#include "linkedList.h" using namespace std; template <class Storeable> linkedList<Storeable>::linkedList() { first = NULL; last = NULL; } template <class Storeable> linkedList<Storeable>::isEmpty() { if(first == NULL) return true; else return false; } . . .
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
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
sorry about that
C++ Syntax (Toggle Plain Text)
template <class Storeable> struct nodeType { Storeable info; nodeType<Storeable> *link; };
Last edited by kyosuke0; Sep 6th, 2009 at 6:51 pm.
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
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?
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
•
•
•
•
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?
•
•
Join Date: Jul 2009
Posts: 972
Reputation:
Solved Threads: 213
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:
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...
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
•
•
•
•
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()
thank you very much DdoubleD
![]() |
Other Threads in the C++ Forum
- Previous Thread: Problem with colors in console
- Next Thread: Help me improve my algo/code
Views: 418 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





