473,587 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with templates

Hi,

I want to implement an graph using templates.
In my header file I define the templates node and edge:

template <class NODE> class GNode
{
NODE *info;
public:
GraphNode();
void SetInfo(const NODE &x);
};

template <class EDGE> class GEdge
{
EDGE *info;
GNode<NODE> *source; // line 75
GNode<NODE> *target; // line 78
public:
GEdge<EDGE>(GNo de* newsource, GNode* newtarget); //line 81
[snip]

File graph.cpp:
[snip]
template <class EDGE>
GEdge<EDGE>::GE dge(GNode* newsource, GNode* newtarget)
{
src = newsource;
trg = newtarget;
info = NULL;
}
[snip]

But the compiler complains:
graph.h:75: type/value mismatch at argument 1 in template parameter list for
`template <class NODE> GNode<NODE>'
graph.h:75: expected a type, got `NODE'
graph.h:75: ANSI C++ forbids declaration `source' with no type
graph.h:78: type/value mismatch at argument 1 in template parameter list for
`template <class NODE> GNode<NODE>'
graph.h:78: expected a type, got `NODE'
graph.h:78: ANSI C++ forbids declaration `target' with no type
graph.h:81: parse error before `*'

Why is class GEdge not accepting "NODE"?

Chris
Jul 23 '05 #1
5 4780

"Christian Christmann" <pl*****@yahoo. de> wrote in message
news:3a******** *****@individua l.net...
Hi,

I want to implement an graph using templates.
In my header file I define the templates node and edge:

template <class NODE> class GNode
{
NODE *info;
public:
GraphNode();
void SetInfo(const NODE &x);
};

template <class EDGE> class GEdge
Did you mean:

template <class EDGE, class NODE> class GEdge
{
EDGE *info;
GNode<NODE> *source; // line 75
GNode<NODE> *target; // line 78
public:
GEdge<EDGE>(GNo de* newsource, GNode* newtarget); //line 81
and:

GEdge(GNode<NOD E>* newsource, GNode<NODE>* newtarget);
[snip]

File graph.cpp:
[snip]
template <class EDGE>
GEdge<EDGE>::GE dge(GNode* newsource, GNode* newtarget)

template <class EDGE, class NODE>
GEdge<EDGE,NODE >::GEdge(GNode< NODE>* newsource, GNode<NODE>* newtarget)
: info( NULL )
, source( newsource )
, target( newtarget )
{}
{
src = newsource;
trg = newtarget;
info = NULL;
}
[snip]

But the compiler complains:
graph.h:75: type/value mismatch at argument 1 in template parameter list
for
`template <class NODE> GNode<NODE>'
graph.h:75: expected a type, got `NODE'
graph.h:75: ANSI C++ forbids declaration `source' with no type
graph.h:78: type/value mismatch at argument 1 in template parameter list
for
`template <class NODE> GNode<NODE>'
graph.h:78: expected a type, got `NODE'
graph.h:78: ANSI C++ forbids declaration `target' with no type
graph.h:81: parse error before `*'

Why is class GEdge not accepting "NODE"?


See above.

Also you'll need to think further about which files the member function
definitions appear in, and how they are included, or you'll need to
explicitly instantiate your classes.

Jeff Flinn
Jul 23 '05 #2
Christian Christmann wrote:
I want to implement an graph using templates.
Is this for exercise sake? I am asking because there are already
libraries for that. Templates, too.
In my header file I define the templates node and edge:

template <class NODE> class GNode
{
NODE *info;
public:
GraphNode();
void SetInfo(const NODE &x);
};

template <class EDGE> class GEdge
{
EDGE *info;
GNode<NODE> *source; // line 75
'NODE' is undefined here. What is 'NODE'? Did you mean to add another
template argument to 'GEdge'?
GNode<NODE> *target; // line 78
public:
GEdge<EDGE>(GNo de* newsource, GNode* newtarget); //line 81
[snip]

File graph.cpp:
[snip]
template <class EDGE>
GEdge<EDGE>::GE dge(GNode* newsource, GNode* newtarget)
{
src = newsource;
trg = newtarget;
info = NULL;
}
[snip]

But the compiler complains:
graph.h:75: type/value mismatch at argument 1 in template parameter list for
`template <class NODE> GNode<NODE>'
graph.h:75: expected a type, got `NODE'
graph.h:75: ANSI C++ forbids declaration `source' with no type
graph.h:78: type/value mismatch at argument 1 in template parameter list for
`template <class NODE> GNode<NODE>'
graph.h:78: expected a type, got `NODE'
graph.h:78: ANSI C++ forbids declaration `target' with no type
graph.h:81: parse error before `*'

Why is class GEdge not accepting "NODE"?


Because there is no such thing as 'NODE' in the scope of 'GEdge'. The
one existing in the 'GNode' template is limited to the scope of 'GNode'
template class definition (and it would mean nothing outside of it,
anyway).

Let me give you a simpler example

template<class T> class OneTemplate { T data; };
template<class T> class AnotherTemplate { T value; };

In these templates each 'T' means a completely different thing. If I
instantiate

OneTemplate<dou ble> otd;

and then

AnotherTemplate <int> ati;

, the 'T' in 'OneTemplate' instantiation will be 'double' and in the
'AnotherTemplat e' instantiation will be 'int'.

Get yourself a book on C++ and study the templates. This is very basic
stuff to seek assistance of a newsgroup for.

V
Jul 23 '05 #3
GEdge(GNode<NOD E>* newsource, GNode<NODE>* newtarget);


Thank you. By the way is the position of '*' important?
I mean, are both statements equal?

GEdge(GNode<NOD E>* newsource, GNode<NODE>* newtarget);
GEdge(GNode<NOD E> *newsource, GNode<NODE> *newtarget);
~~ ~~

Chris

Jul 23 '05 #4
Christian Christmann wrote:
GEdge(GNode<N ODE>* newsource, GNode<NODE>* newtarget);

Thank you. By the way is the position of '*' important?
I mean, are both statements equal?

GEdge(GNode<NOD E>* newsource, GNode<NODE>* newtarget);
GEdge(GNode<NOD E> *newsource, GNode<NODE> *newtarget);
~~ ~~

Chris

Whitespace is ignored in this case.
The '*' used to designate pointers can have as many
spaces before or after without altering the meaning.

int * p;
int* p;
int * p;
int * p;

Yep, all the above are the same.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #5
I want to implement an graph using templates.
Is this for exercise sake?

Yes, it is. I'm new to C++ and need some practice.
I am asking because there are already
libraries for that. Templates, too.
Thank you. I'm going to take a look at them.
V

Chris

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2897
by: Daan | last post by:
Hello all, I'm having a little problem with Smarty. Getting it to work in a simple case went fine, but now I have the following situation: / /smarty /templates template.tpl /templates_c
5
1909
by: Buchleitner Martin | last post by:
Hi! I got another problem parsing my XML document: <document> <paragraph> <style val=listing/> <text>listing #1 text</text> </paragraph> <paragraph>
3
3165
by: Alan Krueger | last post by:
Greetings, I've been able to cache Transformer objects in a Tomcat-based servlet application to avoid unnecessary Transformer rebuilding, except for certain ones on certain machines. I'm running Tomcat 4.1.27 under Eclipse 2.1.0 using the Sysdeo Tomcat plugin using j2re1.4.1_02 under Windows 2000 SP4. I've digested this down to a small...
3
1643
by: bjam | last post by:
Help! The apply-templates function is not currently allowing me to select a specific template... eventhough I tried putting a select statement, it does not seem to work??? Can someone help show how I can select the first template and then have it select others? Below is a sample of the xml as well as the .xsl file when I do something like...
9
3172
by: Fred H | last post by:
I'm currently trying to write a function template that can fill a variable of arbitrary type with 'random' stuff, but I can't seem to get my function template working. In my .h file I've declared the function template like this, inside a class I'm building: template <typename T> T RandBits(); In my .cpp file I've implemented it like...
15
425
by: Rob Ratcliff | last post by:
I'm compiling the latest version of a CORBA ORB called MICO on a Cray X1. It makes heavy use of templates and namespaces. Up until the link step, the C++ source code compiled flawlessly. But, when it tried to link, I got the attached warnings and then an error. Any ideas why the linker wouldn't see the objects in the library? They look like...
3
2389
by: Ali Sahin | last post by:
Hi there, I'd like to transform a XML-File to PDF. The XML-File ist build like followed: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <?xml-stylesheet type="text/xsl" href="D:\app\jboss-3.2.5\server\default\deploy\xifs.war\WEB-INF\classes\de\xifs\resource\xml\de\xifs\resource\xml\dunningaccountreport_de.xsl"?> <!DOCTYPE...
1
1163
by: david | last post by:
Hello, I decided to play a bit with templates and classes, but discovered some problems. So, I have 3 files: LinkedList.h LinkedList.cpp and test.cpp (by the way it is not finished fully) LinkedList.h #ifndef LinkedList_byDavid #define LinkedList_byDavid
8
1743
by: Tim Frink | last post by:
Hi, I want to use a callback function together with templates. Let's say I've this code: File a.h: class A { private:
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
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...
1
7967
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...
0
8220
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...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
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...
0
5392
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...
0
3840
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...

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.