problems with templates

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

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
 
J

Jeff Flinn

Christian Christmann said:
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 said:
{
EDGE *info;
GNode<NODE> *source; // line 75
GNode<NODE> *target; // line 78
public:
GEdge<EDGE>(GNode* newsource, GNode* newtarget); //line 81

and:

GEdge(GNode said:
[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
 
V

Victor Bazarov

Christian said:
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
 
C

Christian Christmann

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
 
T

Thomas Matthews

Christian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top