473,672 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template class compiling problem

I'm trying to write a tree class and I've just stumbled on a compiling
problem. The compiler throws an error on a piece of code which to my eyes
it is error-free. Can anyone help me with this?

Here is the error message:

<error>
/home/rui/development/C++/mytree/src/tree.h:154: error: expected
constructor, destructor, or type conversion before ?&? token
</error>
Here is the code snippet:

<code>
template <class tData>
class tree
{
public:
//snip
class navigator
{
//snip
public:
navigator();
navigator(const navigator&);
~navigator();

//snip

navigator &operator= (const tree<tData> &);
};

};

//snip

template <class tData>
//the offending line: line 154
tree<tData>::na vigator &tree<tData>::n avigator::opera tor= (const tree<tData>
&pointed_nod e)
{
//snip
}
</code>
Thanks in advance
Rui Maciel
--
Running Kubuntu 5.10 with KDE 3.5.1 and proud of it.
jabber:ru****** **@jabber.org
Feb 12 '06 #1
4 1693
Rui Maciel wrote:
I'm trying to write a tree class and I've just stumbled on a compiling
problem. The compiler throws an error on a piece of code which to my
eyes it is error-free. Can anyone help me with this?

Here is the error message:

<error>
/home/rui/development/C++/mytree/src/tree.h:154: error: expected
constructor, destructor, or type conversion before ?&? token
</error>
Here is the code snippet:

<code>
template <class tData>
class tree
{
public:
//snip
class navigator
{
//snip
public:
navigator();
navigator(const navigator&);
~navigator();

//snip

navigator &operator= (const
tree<tData> &); };

};

//snip

template <class tData>
//the offending line: line 154
tree<tData>::na vigator &tree<tData>::n avigator::opera tor= (const
Add "typename " at the beginning.
tree<tData> &pointed_nod e)
{
//snip
}
</code>


V
--
Please remove capital As from my address when replying by mail
Feb 12 '06 #2
Victor Bazarov wrote:
template <class tData>
//the offending line: line 154
tree<tData>::na vigator &tree<tData>::n avigator::opera tor= (const


Add "typename " at the beginning.
tree<tData> &pointed_nod e)
{
//snip
}
</code>


V


Thanks Victor! That did it. Now it compiles perfecly.

But what's the logic behind the "typename" keyword? Why should it be used in
that situation?
Thanks once again for the help Victor.
Rui Maciel
--
Running Kubuntu 5.10 with KDE 3.5.1 and proud of it.
jabber:ru****** **@jabber.org
Feb 12 '06 #3

"Rui Maciel" <ru********@gma il.com> wrote in message
news:43******** *************** @news.telepac.p t...
Victor Bazarov wrote:
template <class tData>
//the offending line: line 154
tree<tData>::na vigator &tree<tData>::n avigator::opera tor= (const


Add "typename " at the beginning.
tree<tData> &pointed_nod e)
{
//snip
}
</code>


V


Thanks Victor! That did it. Now it compiles perfecly.

But what's the logic behind the "typename" keyword? Why should it be used
in
that situation?


The "typename" keyword, in this case, tells the compiler that navigator is a
type. Without typename, it could be mistaken for a static member of tree.

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hc ltech.com>
Feb 13 '06 #4
Rui Maciel wrote:
Victor Bazarov wrote:

template <class tData>
//the offending line: line 154
tree<tData>: :navigator &tree<tData>::n avigator::opera tor= (const


Add "typename " at the beginning.

tree<tData > &pointed_nod e)
{
//snip
}
</code>


V

Thanks Victor! That did it. Now it compiles perfecly.

But what's the logic behind the "typename" keyword? Why should it be used in
that situation?


There is the possibility that tree<>::navigat or is not a type. Although
tree<>::navigat or is defined above, tree<> could possibly be specialized
such that tree<>::navigat or did not exist or was something else.

typename reassures the compiler that you really did mean a type, since
tree<>::navigat or is technically not defined until point of instantiation.

Hope that makes it clearer?
Calum
Feb 13 '06 #5

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

Similar topics

0
1358
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class templates: ------------------------------------------ in file "Model.h":
4
2339
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a .h and a .cpp file, gcc throws some strange linking errors. Compiling like this:
2
3337
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template class B has an instance of template class A, which has some base type T (usually int or double). However, the base type T is important to calculations in B, so I would like to obtain access to the type for further variable declaration within B. ...
6
1421
by: B. Williams | last post by:
I have a problem dealing with class template where I was to write a class and after submitting the class, this is the feedback I got back from the instructor. I don't really understand it. Can someone please assist me. Your constructor needs to do something. It should take in 4 values and pass these to the individual set functions. I think if you go on to write main you'll see the compiler errors and they should help. I did write...
45
2886
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
8
7623
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will be successful but not linking. Can somebody help me in telling the exact reason why compiler cannot find the reference of template class member function definition defined in cpp file?? following problem is not linking (undefined reference of...
6
2341
by: bogus1one | last post by:
Hi All Given the following: // NamespaceTemplate.cpp : Defines the entry point for the console application. // #include "stdafx.h"
3
1716
by: nick4ng | last post by:
If I have a templated constructor, there is a way to know, at compile time, the instance when compiling the destructor? To be clearer my problem is that I have a templated constructor that is empty for particular arguments. I need to have an empty destructor in this case as well. Is it possible? I should add that for other reasons I can not have a templated class.
7
5765
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
8485
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
8403
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
8930
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
8828
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
4227
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
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
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
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.