473,783 Members | 2,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me generic>

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

void SetNextLink(Nod e *xiNode){NextNo de = xiNode;}
void SetPrevLink(Nod e *xiNode){PrevNo de = xiNode;}

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

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){retu rn 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 2371

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:br******** **@news.freedom 2surf.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<typena me generic>

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

void SetNextLink(Nod e *xiNode){NextNo de = xiNode;}
void SetPrevLink(Nod e *xiNode){PrevNo de = xiNode;}

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

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){retu rn 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<typena me generic>

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

void SetNextLink(Nod e *xiNode){NextNo de = xiNode;}
void SetPrevLink(Nod e *xiNode){PrevNo de = xiNode;}

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

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){retu rn 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.co m> wrote in message
news:br******** ****@ID-192448.news.uni-berlin.de...

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:br******** **@news.freedom 2surf.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<typena me generic>

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

void SetNextLink(Nod e *xiNode){NextNo de = xiNode;}
void SetPrevLink(Nod e *xiNode){PrevNo de = xiNode;}

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

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){retu rn 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:\Documen ts and Settings\Allan\ My
Documents\Progs \QueueStack\Dou bleList.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*****@TAKEAW AYf2s.com> wrote in message
news:br******** *@news.freedom2 surf.net...

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

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:br******** **@news.freedom 2surf.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<typena me generic>

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

void SetNextLink(Nod e *xiNode){NextNo de = xiNode;}
void SetPrevLink(Nod e *xiNode){PrevNo de = xiNode;}

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

void SetData(generic *xiData){Data = xiData;}
generic *GetData(){retu rn 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:\Documen ts and Settings\Allan\ My
Documents\Progs \QueueStack\Dou bleList.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...assumin g T as the template parameter.

So, to access it outside the class-
template<typena me T>
Node<T> *Node<T>::GetPr evLink(){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<typena me generic>

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

void SetNextLink(Nod e *xiNode){NextNo de = xiNode;}
void SetPrevLink(Nod e *xiNode){PrevNo de = xiNode;}

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

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

private:
Node *NextNode;
Node *PrevNode;

generic *Data;

bool Tail;
bool Head;
};

template<typena me T>
Node<T> *Node<T>::GetPr evLink(){return PrevNode;}

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

HTH,
J.Schafer
Jul 22 '05 #6

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

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:br******** *@news.freedom2 surf.net...

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

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:br******** **@news.freedom 2surf.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<typena me generic>
>
> class Node
> {
> public:
> Node();
> Node(char xiType);
> virtual ~Node();
>
> void SetNextLink(Nod e *xiNode){NextNo de = xiNode;}
> void SetPrevLink(Nod e *xiNode){PrevNo de = xiNode;}
>
> Node *GetPrevLink(){ return PrevNode;}
> Node *GetNextLink(){ return NextNode;}
>
> void SetData(generic *xiData){Data = xiData;}
> generic *GetData(){retu rn 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:\Documen ts and Settings\Allan\ My
Documents\Progs \QueueStack\Dou bleList.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...assumin g T as the template

parameter.
So, to access it outside the class-
template<typena me T>
Node<T> *Node<T>::GetPr evLink(){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:\Documen ts and Settings\Allan\ My Documents\Progs \QueueStack\Sta ck.cpp(4):
error C2955: 'Stack' : use of class template requires template argument
list"
I tried to make this Stack::Stack<ge neric>() 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*****@TAKEAW AYf2s.com> wrote in message
news:br******** *@news.freedom2 surf.net...

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

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
news:br******** *@news.freedom2 surf.net...

"Josephine Schafer" <no************ ****@hotmail.co m> wrote in message
news:br******** ****@ID-192448.news.uni-berlin.de...
>
> "Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message
> news:br******** **@news.freedom 2surf.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<typena me generic>
> >
> > class Node
> > {
> > public:
> > Node();
> > Node(char xiType);
> > virtual ~Node();
> >
> > void SetNextLink(Nod e *xiNode){NextNo de = xiNode;}
> > void SetPrevLink(Nod e *xiNode){PrevNo de = xiNode;}
> >
> > Node *GetPrevLink(){ return PrevNode;}
> > Node *GetNextLink(){ return NextNode;}
> >
> > void SetData(generic *xiData){Data = xiData;}
> > generic *GetData(){retu rn 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:\Documen ts and Settings\Allan\ My
Documents\Progs \QueueStack\Dou bleList.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...assumin g T as the template

parameter.

So, to access it outside the class-
template<typena me T>
Node<T> *Node<T>::GetPr evLink(){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<typena me T>
class Stack{
public:
Stack();
void push(T t);
....
};

template<typena me T>
Stack<T>::Stack ()
{
// Place your code
}
template<typena me 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
8605
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
6399
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. How do I do this? Also, I want to have a data member for
3
1825
by: Lord Labakudas | last post by:
Hi, I have the following simple template implementation: // -------------- b.h ----------------- // template <class t> class b { public: b() ;
16
16291
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
2169
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 XP Pro SP 1, .Net Framework 1.1) and on our production server (Windows 2K SP 4, .Net Framework 1.1). I have simplified the code and data to isolate the problem. When I use the xsl:strip-space (Line 12) declaration in conjunction with the xsl:sort...
2
7403
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* JavaScript. Still, I have managed to get something together, which I am putting across here. Any help (even pointing me to the place to look) is welcome. The problem I have is as follows:
1
3075
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
2203
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 the .cpp files. After some reading, I found that the only way to do this is to add the actual template instantiations in the .cpp file. But, how do you do that if you're not working with built-in types? For example, a template class might be,
1
2390
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 producing high performance C++ code; code that is provably correct. The issue here is orthogonal to the question of expression templates, which at present seem to me to be relatively simple.
4
2028
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 information at runtime after reading a file. After reading the file, several objects of a templated ClassA are created. I thought that the way to do this is to create an abstract class AbstractClassA and have all templated classes inherit from the...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.