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:

#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:

#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

Recommended Answers

All 8 Replies

You forgot to include the contents of: "nodeType.h"

commented: very helpful +1

You forgot to include the contents of: "nodeType.h"

sorry about that

template <class Storeable>
struct nodeType
{
	Storeable info;
	nodeType<Storeable> *link;
};

What is the definition of Storeable? You declare types, but I don't see the definition.

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?

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.

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()

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

Anytime. Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.