By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,742 Members | 2,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,742 IT Pros & Developers. It's quick & easy.

Using Class Templates

P: n/a
I have had a look through the FAQ and found that if I am using a class
template then I need an argument list. I have tried to add this but it is
not quite working - i.e. it doesnt compile. My code is below, could
somebody please point me in the right direction?
Thanks
Allan

#include <iostream>

template<typename generic>

class Node
{
public:
Node();
Node(char xiType);
virtual ~Node();

void SetNextLink(Node *xiNode){NextNode = xiNode;}
void SetPrevLink(Node *xiNode){PrevNode = xiNode;}

Node *GetPrevLink(){return PrevNode;}
Node *GetNextLink(){return NextNode;}

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){return Data;}

private:
Node *NextNode;
Node *PrevNode;

generic *Data;

bool Tail;
bool Head;
};

Node<int> NodeInt;
Node<float> NodeFloat;
Node<double> NodeDouble;
Node<char> NodeChar;
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:br**********@news.freedom2surf.net...
I have had a look through the FAQ and found that if I am using a class
template then I need an argument list. I have tried to add this but it is
not quite working - i.e. it doesnt compile. My code is below, could
somebody please point me in the right direction?
Thanks
Allan

#include <iostream>

template<typename generic>

class Node
{
public:
Node();
Node(char xiType);
virtual ~Node();

void SetNextLink(Node *xiNode){NextNode = xiNode;}
void SetPrevLink(Node *xiNode){PrevNode = xiNode;}

Node *GetPrevLink(){return PrevNode;}
Node *GetNextLink(){return NextNode;}

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){return Data;}

private:
Node *NextNode;
Node *PrevNode;

generic *Data;

bool Tail;
bool Head;
};

Node<int> NodeInt;
Node<float> NodeFloat;
Node<double> NodeDouble;
Node<char> NodeChar;


What's the error message?
It compiles fine on Comeau online.
Jul 22 '05 #2

P: n/a
On Fri, 12 Dec 2003 10:47:52 +0000, Allan Bruce wrote:
I have had a look through the FAQ and found that if I am using a class
template then I need an argument list. I have tried to add this but it is
not quite working - i.e. it doesnt compile. My code is below, could
somebody please point me in the right direction?
Thanks
Allan

#include <iostream>

template<typename generic>

class Node
{
public:
Node();
Node(char xiType);
virtual ~Node();

void SetNextLink(Node *xiNode){NextNode = xiNode;}
void SetPrevLink(Node *xiNode){PrevNode = xiNode;}

Node *GetPrevLink(){return PrevNode;}
Node *GetNextLink(){return NextNode;}

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){return Data;}

private:
Node *NextNode;
Node *PrevNode;

generic *Data;

bool Tail;
bool Head;
};

Node<int> NodeInt;
Node<float> NodeFloat;
Node<double> NodeDouble;
Node<char> NodeChar;


As I understand it, and I'm far from an expert, you need to start
template <class identifier>
and then use identifier inside the class definition/ declaration, so

template <class T>

class Node {
....
void setData (T *xidata) ...

private:
T *Data;
....
}

Now when you specify, say, Node<int>, Data is int*, setData(int * xiData)
and so on.

Tony
Jul 22 '05 #3

P: n/a

"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:br************@ID-192448.news.uni-berlin.de...

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:br**********@news.freedom2surf.net...
I have had a look through the FAQ and found that if I am using a class
template then I need an argument list. I have tried to add this but it is not quite working - i.e. it doesnt compile. My code is below, could
somebody please point me in the right direction?
Thanks
Allan

#include <iostream>

template<typename generic>

class Node
{
public:
Node();
Node(char xiType);
virtual ~Node();

void SetNextLink(Node *xiNode){NextNode = xiNode;}
void SetPrevLink(Node *xiNode){PrevNode = xiNode;}

Node *GetPrevLink(){return PrevNode;}
Node *GetNextLink(){return NextNode;}

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){return Data;}

private:
Node *NextNode;
Node *PrevNode;

generic *Data;

bool Tail;
bool Head;
};

Node<int> NodeInt;
Node<float> NodeFloat;
Node<double> NodeDouble;
Node<char> NodeChar;


What's the error message?
It compiles fine on Comeau online.


The error is when I try to use one of the above methods, in my DoubleList.h
I have the following line

Node *GetLastNode(){return mTail->GetPrevLink();}

And the error message is
"f:\Documents and Settings\Allan\My
Documents\Progs\QueueStack\DoubleList.h(22): error C2955: 'Node' : use of
class template requires template argument list"
I have a lot of these error messages, as I am accessing Node pointers a lot
Thanks
Allan
Jul 22 '05 #4

P: n/a

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:br*********@news.freedom2surf.net...

"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:br************@ID-192448.news.uni-berlin.de...

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:br**********@news.freedom2surf.net...
I have had a look through the FAQ and found that if I am using a class
template then I need an argument list. I have tried to add this but it is not quite working - i.e. it doesnt compile. My code is below, could
somebody please point me in the right direction?
Thanks
Allan

#include <iostream>

template<typename generic>

class Node
{
public:
Node();
Node(char xiType);
virtual ~Node();

void SetNextLink(Node *xiNode){NextNode = xiNode;}
void SetPrevLink(Node *xiNode){PrevNode = xiNode;}

Node *GetPrevLink(){return PrevNode;}
Node *GetNextLink(){return NextNode;}

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){return Data;}

private:
Node *NextNode;
Node *PrevNode;

generic *Data;

bool Tail;
bool Head;
};

Node<int> NodeInt;
Node<float> NodeFloat;
Node<double> NodeDouble;
Node<char> NodeChar;


What's the error message?
It compiles fine on Comeau online.


The error is when I try to use one of the above methods, in my DoubleList.h
I have the following line

Node *GetLastNode(){return mTail->GetPrevLink();}

And the error message is
"f:\Documents and Settings\Allan\My
Documents\Progs\QueueStack\DoubleList.h(22): error C2955: 'Node' : use of
class template requires template argument list"


The error messages speak the reply for you.
Node is NOT a class but a class template.
So you can't return pointer to Node.
Instead Node<T> is a concrete class...assuming T as the template parameter.

So, to access it outside the class-
template<typename T>
Node<T> *Node<T>::GetPrevLink(){return PrevNode;}
HTH,
J.Schafer
Jul 22 '05 #5

P: n/a
So, to access it outside the class-

Read access as define.

Try this -
#include <iostream>

template<typename generic>

class Node
{
public:
Node();
Node(char xiType);
virtual ~Node();

void SetNextLink(Node *xiNode){NextNode = xiNode;}
void SetPrevLink(Node *xiNode){PrevNode = xiNode;}

Node *GetNextLink(){return NextNode;}
Node *GetPrevLink();

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){return Data;}

private:
Node *NextNode;
Node *PrevNode;

generic *Data;

bool Tail;
bool Head;
};

template<typename T>
Node<T> *Node<T>::GetPrevLink(){return PrevNode;}

Node<int> NodeInt;
Node<int>* t =NodeInt.GetPrevLink();

HTH,
J.Schafer
Jul 22 '05 #6

P: n/a

"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:br************@ID-192448.news.uni-berlin.de...

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:br*********@news.freedom2surf.net...

"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:br************@ID-192448.news.uni-berlin.de...

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:br**********@news.freedom2surf.net...
> I have had a look through the FAQ and found that if I am using a class > template then I need an argument list. I have tried to add this but
it is
> not quite working - i.e. it doesnt compile. My code is below, could
> somebody please point me in the right direction?
> Thanks
> Allan
>
>
>
> #include <iostream>
>
> template<typename generic>
>
> class Node
> {
> public:
> Node();
> Node(char xiType);
> virtual ~Node();
>
> void SetNextLink(Node *xiNode){NextNode = xiNode;}
> void SetPrevLink(Node *xiNode){PrevNode = xiNode;}
>
> Node *GetPrevLink(){return PrevNode;}
> Node *GetNextLink(){return NextNode;}
>
> void SetData(generic *xiData){Data = xiData;}
> generic *GetData(){return Data;}
>
> private:
> Node *NextNode;
> Node *PrevNode;
>
> generic *Data;
>
> bool Tail;
> bool Head;
> };
>
> Node<int> NodeInt;
> Node<float> NodeFloat;
> Node<double> NodeDouble;
> Node<char> NodeChar;
>

What's the error message?
It compiles fine on Comeau online.
The error is when I try to use one of the above methods, in my DoubleList.h I have the following line

Node *GetLastNode(){return mTail->GetPrevLink();}

And the error message is
"f:\Documents and Settings\Allan\My
Documents\Progs\QueueStack\DoubleList.h(22): error C2955: 'Node' : use of class template requires template argument list"


The error messages speak the reply for you.
Node is NOT a class but a class template.
So you can't return pointer to Node.
Instead Node<T> is a concrete class...assuming T as the template

parameter.
So, to access it outside the class-
template<typename T>
Node<T> *Node<T>::GetPrevLink(){return PrevNode;}
HTH,
J.Schafer


Thanks that did, I am trying to implement a generic stack and queue. I am
testing them out using the following code:

Stack<int> *lIntStack;// instantiate a new Stack of ints
Stack<double> *lDblStack;// instantiate a new Stack of doubles
Queue<char> *lCharQueue;// instantiate a new Queue of chars

lIntStack = new Stack<int>();// allocate memory to stack of ints
lDblStack = new Stack<double>();// allocate memory to stack of doubles
lCharQueue = new Queue<char>();// allocate memory to queue of chars

I am getting a compiler error in my Stack.cpp. The code around the error
is:

#include <iostream>
#include "Stack.h"

Stack::Stack()
{
mDataList = NULL;
mDataList = new DoubleList();
if (!mDataList)
return;
}

And my error is:
"f:\Documents and Settings\Allan\My Documents\Progs\QueueStack\Stack.cpp(4):
error C2955: 'Stack' : use of class template requires template argument
list"
I tried to make this Stack::Stack<generic>() but that didnt work - it
complains that its an undeclared identifier.
I am aware that my use of "new DoubleList" is going to require an arguemnet
aswell, but unsure of how to do it.
Where am I going wrong?
Thanks
Allan
Jul 22 '05 #7

P: n/a

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:br*********@news.freedom2surf.net...

"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:br************@ID-192448.news.uni-berlin.de...

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:br*********@news.freedom2surf.net...

"Josephine Schafer" <no****************@hotmail.com> wrote in message
news:br************@ID-192448.news.uni-berlin.de...
>
> "Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
> news:br**********@news.freedom2surf.net...
> > I have had a look through the FAQ and found that if I am using a class > > template then I need an argument list. I have tried to add this but it is
> > not quite working - i.e. it doesnt compile. My code is below, could
> > somebody please point me in the right direction?
> > Thanks
> > Allan
> >
> >
> >
> > #include <iostream>
> >
> > template<typename generic>
> >
> > class Node
> > {
> > public:
> > Node();
> > Node(char xiType);
> > virtual ~Node();
> >
> > void SetNextLink(Node *xiNode){NextNode = xiNode;}
> > void SetPrevLink(Node *xiNode){PrevNode = xiNode;}
> >
> > Node *GetPrevLink(){return PrevNode;}
> > Node *GetNextLink(){return NextNode;}
> >
> > void SetData(generic *xiData){Data = xiData;}
> > generic *GetData(){return Data;}
> >
> > private:
> > Node *NextNode;
> > Node *PrevNode;
> >
> > generic *Data;
> >
> > bool Tail;
> > bool Head;
> > };
> >
> > Node<int> NodeInt;
> > Node<float> NodeFloat;
> > Node<double> NodeDouble;
> > Node<char> NodeChar;
> >
>
> What's the error message?
> It compiles fine on Comeau online.
>
>

The error is when I try to use one of the above methods, in my DoubleList.h I have the following line

Node *GetLastNode(){return mTail->GetPrevLink();}

And the error message is
"f:\Documents and Settings\Allan\My
Documents\Progs\QueueStack\DoubleList.h(22): error C2955: 'Node' : use of class template requires template argument list"


The error messages speak the reply for you.
Node is NOT a class but a class template.
So you can't return pointer to Node.
Instead Node<T> is a concrete class...assuming T as the template

parameter.

So, to access it outside the class-
template<typename T>
Node<T> *Node<T>::GetPrevLink(){return PrevNode;}
HTH,
J.Schafer


Thanks that did, I am trying to implement a generic stack and queue. I am
testing them out using the following code:

Stack<int> *lIntStack;// instantiate a new Stack of ints
Stack<double> *lDblStack;// instantiate a new Stack of doubles
Queue<char> *lCharQueue;// instantiate a new Queue of chars

lIntStack = new Stack<int>();// allocate memory to stack of ints
lDblStack = new Stack<double>();// allocate memory to stack of doubles
lCharQueue = new Queue<char>();// allocate memory to queue of chars

I am getting a compiler error in my Stack.cpp. The code around the error
is:

#include <iostream>
#include "Stack.h"

Stack::Stack()


The problem is here.
Stack is NOT a class but a class template.
Understand that the compilation model of templates is different.
Read about the inclusive/separate compilation models.
Assuming inclusive model.
Place all your template code in a .h file.
So it should be (.h file) -
template<typename T>
class Stack{
public:
Stack();
void push(T t);
....
};

template<typename T>
Stack<T>::Stack()
{
// Place your code
}
template<typename T>
void Stack<T>::push(T t)
{
// Place your code
}

In your implementation file (Stack.cpp)
1. Include your .h file

int main ()
{
Stack<int> InstStack;
InstStack.push(1);
InstStack.push(12);
}

so on...

HTH,
J.Schafer


Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.