| | |
compiler error code
Please support our C++ advertiser: Intel Parallel Studio Home
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: 920
Reputation:
Solved Threads: 147
•
•
•
•
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: 920
Reputation:
Solved Threads: 147
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
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





