473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Using Class Templates

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
7 2350

"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
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

"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

"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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
4
by: Gert Van den Eynde | last post by:
Hi all, A beginners question.... I've got a template class template <class T> classA {...} In an other class, I want to pass a pointer to an instance of classA as a function argument....
3
by: Lord Labakudas | last post by:
Hi, I have the following simple template implementation: // -------------- b.h ----------------- // template <class t> class b { public: b() ;
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows...
2
by: Scamjunk | last post by:
I have been desperately looking for a treeview-type solution for my problem for the past three weeks and have been greatly unsuccessful. I am totally new to the world of XSLT and I *don't know*...
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
14
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to...
1
by: Ted | last post by:
I have cross posted this to comp.lang.c++ and to sci.math.num- analysis in the belief that the topic is of interest to some in both groups. I am building my toolkit, in support of my efforts in...
4
by: aaragon | last post by:
Hello all, I think the problem I'm facing is hard so I wonder if someone faced it before and if there is a simple solution to it. I am creating a program that basically takes all the required...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.