473,394 Members | 1,802 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,394 software developers and data experts.

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>(GNode* newsource, GNode* newtarget); //line 81
[snip]

File graph.cpp:
[snip]
template <class EDGE>
GEdge<EDGE>::GEdge(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 4762

"Christian Christmann" <pl*****@yahoo.de> wrote in message
news:3a*************@individual.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>(GNode* newsource, GNode* newtarget); //line 81
and:

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

File graph.cpp:
[snip]
template <class EDGE>
GEdge<EDGE>::GEdge(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>(GNode* newsource, GNode* newtarget); //line 81
[snip]

File graph.cpp:
[snip]
template <class EDGE>
GEdge<EDGE>::GEdge(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<double> otd;

and then

AnotherTemplate<int> ati;

, the 'T' in 'OneTemplate' instantiation will be 'double' and in the
'AnotherTemplate' 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<NODE>* newsource, GNode<NODE>* newtarget);


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

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

Chris

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

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

GEdge(GNode<NODE>* newsource, GNode<NODE>* newtarget);
GEdge(GNode<NODE> *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.learn.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
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
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
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...
3
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...
9
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...
15
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...
3
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"...
1
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) ...
8
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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
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...

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.