|
10 Dec 2005, 07:19
|
#1
|
Aria's TeddyBear :p
Join Date: Apr 2002
Location: Rhode Island, USA
Posts: 516
|
trouble with C++ templates
For one of my CS classes we have to take a c++ project and turn it into a template project. So i was doing so but could not get past this error and i can't find any reason as to why
Orignal project URl
http://e-illuminati.com/~ryan/proj.tar
Code:
g++ -c Queue.cpp
In file included from Queue.cpp:1:
Queue.h:6: error: expected class-name before '{' token
Queue.cpp:17: error: `template<class T> class Queue' used without template parameters
Queue.cpp:22: error: `template<class T> class Queue' used without template parameters
Queue.cpp: In function `T dequeue()':
Queue.cpp:24: error: there are no arguments to `removeHead' that depend on a template parameter, so a declaration of `removeHead' must be available
Queue.cpp:24: error: (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
make: *** [Queue.o] Error 1
Queue.h
Code:
#ifndef QUEUE_CLASS
#define QUEUE_CLASS
#include "List.h"
template <typename T>
class Queue : public List {
private:
public:
Queue();
~Queue<T>();
void enqueue(T);
T dequeue();
};
#endif
Queue.cpp
Code:
#include "Queue.h"
// Queue Class method implementations
template <typename T>
Queue<T>::Queue()
{
}
// Class Destructor
template <typename T>
Queue<T>::~Queue()
{
}
template <typename T>
void Queue::enqueue(T c)
{
appendTail(c);
}
template <typename T>
T Queue::dequeue()
{
return removeHead();
}
template class Queue<char>;
List.h
Code:
#ifndef LIST_CLASS
#define LIST_CLASS
#include "Node.h"
template <typename T>
class List {
private:
Node<T>* head; // pointer to the head of the list
Node<T>* tail; // pointer to teh tail of the list
public:
List(); // default constructor
~List<T>(); // default destructor
bool isEmpty(); // test for empty list
// return true if list is empty
void appendTail(T); // create node to hold character
// then add to list after tail
void appendHead(T); // create node to hold character
// then add to list before head
T removeTail(); // return character in the tail node then
// remove node from list and destroy it
T removeHead(); // return character in the head node then
// remove node from list and destroy it
};
template class List<char>;
#endif
__________________
Proud to be have been Fyre, NewDawn, NoS - The Illuminati, [ 1u p]
R3 [Acid] peon
R4 - R7 [Fyre] HC
R7 - R8 [ND] HC
R8 - R13 [NoS] MC
R14 - R16 [ 1u p] MO
R17 Retired
Last edited by Doorsdown; 10 Dec 2005 at 17:48.
Reason: updated after fixing 1 of the errors...ty rob
|
|
|
10 Dec 2005, 08:20
|
#2
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: trouble with C++ templates
i dont think template code can be in .cpp files, only .h files.
|
|
|
10 Dec 2005, 13:21
|
#3
|
wasted
Join Date: Dec 2000
Location: Under the floorboards
Posts: 1,240
|
Re: trouble with C++ templates
class Queue : public List in queue.h should read:
class Queue : public List<T> since List is a template class.
appendTail(c);
What is the variable c? I can't see it declared anywhere.
Quote:
Originally Posted by Phil^
i dont think template code can be in .cpp files, only .h files.
|
That sounds like some kind of Java-ism. It's certainly not how C/C++ typically work.
__________________
“They were totally confused,” said the birdman, whose flying suit gives him a passing resemblance to Buzz Lightyear in Toy Story. “The authorities said that I was an unregistered aircraft and to fly, you need a licence. I told them, ‘No. To fly, you need wings’.”
|
|
|
10 Dec 2005, 17:45
|
#4
|
Aria's TeddyBear :p
Join Date: Apr 2002
Location: Rhode Island, USA
Posts: 516
|
Re: trouble with C++ templates
Quote:
Originally Posted by ComradeRob
class Queue : public List in queue.h should read:
class Queue : public List<T> since List is a template class.
appendTail(c);
What is the variable c? I can't see it declared anywhere.
That sounds like some kind of Java-ism. It's certainly not how C/C++ typically work.
|
thank you rob that took out one error and yes template codes does go into the cpp file
__________________
Proud to be have been Fyre, NewDawn, NoS - The Illuminati, [ 1u p]
R3 [Acid] peon
R4 - R7 [Fyre] HC
R7 - R8 [ND] HC
R8 - R13 [NoS] MC
R14 - R16 [ 1u p] MO
R17 Retired
|
|
|
10 Dec 2005, 18:07
|
#5
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Re: trouble with C++ templates
Quote:
Originally Posted by Doorsdown
thank you rob that took out one error and yes template codes does go into the cpp file
|
You've also missed out the <T> after Queue in these lines:
void Queue::enqueue(T c)
T Queue::dequeue()
On the other hand, ~Queue<T>() in the header is overkill - you don't need the <T> there.
__________________
#linux
|
|
|
10 Dec 2005, 19:22
|
#6
|
Aria's TeddyBear :p
Join Date: Apr 2002
Location: Rhode Island, USA
Posts: 516
|
Re: trouble with C++ templates
thank you queball
the one error i can't shake is
Code:
Queue.h:6: error: expected class-name before '{' token
__________________
Proud to be have been Fyre, NewDawn, NoS - The Illuminati, [ 1u p]
R3 [Acid] peon
R4 - R7 [Fyre] HC
R7 - R8 [ND] HC
R8 - R13 [NoS] MC
R14 - R16 [ 1u p] MO
R17 Retired
|
|
|
11 Dec 2005, 03:59
|
#7
|
wasted
Join Date: Dec 2000
Location: Under the floorboards
Posts: 1,240
|
Re: trouble with C++ templates
I think you might need to change the class declaration to: class Queue<T> : public List<queue>
A decent C++ manual would probably be more help than an internet forum though
__________________
“They were totally confused,” said the birdman, whose flying suit gives him a passing resemblance to Buzz Lightyear in Toy Story. “The authorities said that I was an unregistered aircraft and to fly, you need a licence. I told them, ‘No. To fly, you need wings’.”
|
|
|
11 Dec 2005, 08:19
|
#8
|
Aria's TeddyBear :p
Join Date: Apr 2002
Location: Rhode Island, USA
Posts: 516
|
Re: trouble with C++ templates
yes but this is free and i havn't been able to find any good online documentation on templates thus far
Quote:
Originally Posted by ComradeRob
I think you might need to change the class declaration to: class Queue<T> : public List<queue>
|
Code:
Queue.h:6: error: `Queue' is not a template
Queue.h:6: error: `queue' was not declared in this scope
Queue.h:6: error: template argument 1 is invalid
__________________
Proud to be have been Fyre, NewDawn, NoS - The Illuminati, [ 1u p]
R3 [Acid] peon
R4 - R7 [Fyre] HC
R7 - R8 [ND] HC
R8 - R13 [NoS] MC
R14 - R16 [ 1u p] MO
R17 Retired
|
|
|
11 Dec 2005, 08:28
|
#9
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: trouble with C++ templates
class Queue<T> : public List<T> ??
|
|
|
11 Dec 2005, 08:34
|
#10
|
Aria's TeddyBear :p
Join Date: Apr 2002
Location: Rhode Island, USA
Posts: 516
|
Re: trouble with C++ templates
Quote:
Originally Posted by Phil^
class Queue<T> : public List<T> ??
|
it was
class Queue : public List<T>
__________________
Proud to be have been Fyre, NewDawn, NoS - The Illuminati, [ 1u p]
R3 [Acid] peon
R4 - R7 [Fyre] HC
R7 - R8 [ND] HC
R8 - R13 [NoS] MC
R14 - R16 [ 1u p] MO
R17 Retired
|
|
|
14 Dec 2005, 03:05
|
#11
|
Aria's TeddyBear :p
Join Date: Apr 2002
Location: Rhode Island, USA
Posts: 516
|
Re: trouble with C++ templates
i got it all working...yay
things like List<T>::removeHead(); where needed
plus finding all the spots where i had syntax errors
thanks guys
__________________
Proud to be have been Fyre, NewDawn, NoS - The Illuminati, [ 1u p]
R3 [Acid] peon
R4 - R7 [Fyre] HC
R7 - R8 [ND] HC
R8 - R13 [NoS] MC
R14 - R16 [ 1u p] MO
R17 Retired
|
|
|
|
All times are GMT +1. The time now is 14:56.
| |