template class compiling problem

R

Rui Maciel

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>::navigator &tree<tData>::navigator::eek:perator= (const tree<tData>
&pointed_node)
{
//snip
}
</code>


Thanks in advance
Rui Maciel
 
V

Victor Bazarov

Rui said:
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>::navigator &tree<tData>::navigator::eek:perator= (const

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

V
 
R

Rui Maciel

Victor said:
Add "typename " at the beginning.


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
 
S

Sumit Rajan

Rui Maciel said:
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.
 
C

Calum Grant

Rui said:
Victor Bazarov wrote:




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<>::navigator is not a type. Although
tree<>::navigator is defined above, tree<> could possibly be specialized
such that tree<>::navigator did not exist or was something else.

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

Hope that makes it clearer?
Calum
 

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
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top