473,785 Members | 2,746 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 4792

"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
2916
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
1922
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
3180
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 (albeit convoluted) sample that exhibits the behavior reliably on my development workstation.
3
1657
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 the following in the main section it does not seem to work, it only works when I remove the select...
9
3190
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 this:
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 pretty long names, so maybe there is some type of symbol length or mangling issue going on? The...
3
2400
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 entities >
1
1171
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
1752
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
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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...
0
9947
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...
0
6737
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
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
3
2877
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.