By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

problems with templates

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.