Using Class Templates

A

Allan Bruce

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;
 
J

Josephine Schafer

Allan Bruce said:
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.
 
T

Tony Burrows

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
 
A

Allan Bruce

Josephine Schafer said:
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
 
J

Josephine Schafer

Allan Bruce said:
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
 
J

Josephine Schafer

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
 
A

Allan Bruce

Josephine Schafer said:
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
 
J

Josephine Schafer

Allan Bruce said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top