C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template class not recognized

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Patrick Leslie Polzer
Guest





PostPosted: Thu Jan 13, 2005 8:39 pm    Post subject: Template class not recognized Reply with quote



Hello,

I have a problem with templates which I wasn't able to solve yet.
The error message reads as follows:

btree.hxx:129: error: expected constructor, destructor, or type
conversion before '*' token
btree.hxx:129: error: expected `;' before '*' token

With the offending code line 129 with context being:

template <typename K, typename D>
MacroNode* BTree<K,D>::insert ( MacroNode* n, Node* m ) /* 129 */
{
/* method impl goes here */
}

But MacroNode<K,D> is well-defined in the included header file
"macronode.hxx":

template <typename K, typename D>
class MacroNode
{
/* ... */
}

and MacroNode is typedef'd thus:

template <typename K, typename D>
class BTree
{
typedef MacroNode<K,D> MacroNode;
/* ... */
}

I can't understand this - all seems correct. If I replace
'MacroNode' in line 129 with 'MacroNode<K,D>', the compiler
(gcc 3.4.3) complains about MacroNode not being a template:

btree.hxx:129: error: `MacroNode<K, D>' is not a template

If you need to know more about the code, you can view all of it
(including a tarball with all four files) at
http://nic-nac-project.de/~skypher/btree/


Kind regards and many thanks in advance,

Leslie

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gianni Mariani
Guest





PostPosted: Fri Jan 14, 2005 11:03 am    Post subject: Re: Template class not recognized Reply with quote



Patrick Leslie Polzer wrote:
Quote:
Hello,

I have a problem with templates which I wasn't able to solve yet.
The error message reads as follows:

btree.hxx:129: error: expected constructor, destructor, or type
conversion before '*' token
btree.hxx:129: error: expected `;' before '*' token

With the offending code line 129 with context being:

That's not really the offending line.

You have these:
typedef MacroNode<K,D> MacroNode;
typedef Node<K,D> Node;


Which change the defintion of MacroNode and Node depending on context.

After changing all the instances of Node to Node<K,D> and MacroNode to
MacroNode<K,D>, you'll get past that error (which I did) and land you
into another one:

Node<K,D>** p = &(n->nodes);

error: cannot convert `Node<int, std::string>***' to `Node<int,
std::string>**' in initialization

And when you fix that, you'll end up with another:

p[i-1]->right = insert ( p[i-1]->right, m );
error: no matching function for call to `BTree<int,
std::string>::insert(Node<int, std::string>**&, Node<int,
std::string>*&)'


etc etc ...

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Sat Jan 15, 2005 2:50 am    Post subject: Re: Template class not recognized Reply with quote



Patrick Leslie Polzer wrote:

Quote:
I have a problem with templates which I wasn't able to solve yet.
The error message reads as follows:

btree.hxx:129: error: expected constructor, destructor, or type
conversion before '*' token
btree.hxx:129: error: expected `;' before '*' token

With the offending code line 129 with context being:

template <typename K, typename D
MacroNode* BTree {
/* method impl goes here */
}

But MacroNode<K,D> is well-defined in the included header file
"macronode.hxx":

template <typename K, typename D
class MacroNode
{
/* ... */
}

Except, of course, that that isn't the MacroNode you expect to
use:-).

Quote:
and MacroNode is typedef'd thus:

template class BTree
{
typedef MacroNode

This is. Note that it is defined within the class. Using it
within the class is (or should be) no problem, but if you use it
outside the class, you have to write:

BTree<K,D>::MacroNode

The return type of a function definition outside of the class is
outside the class. So your line 129 should be:

template< typename K, typename D >
BTree< K, D >::MacroNode*
BTree< K, D >::insert( ...

Quote:
/* ... */
}

I can't understand this - all seems correct. If I replace
'MacroNode' in line 129 with 'MacroNode<K,D>', the compiler
(gcc 3.4.3) complains about MacroNode not being a template:

There are two instances of MacroNode in your line 129. The
first is outside the class, and only the global, templated
MacroNode is visible. The second is inside the class, and the
non-template typedef hides the global definition. If you change
both, it's normal that the compiler complain.

Quote:
btree.hxx:129: error: `MacroNode<K, D>' is not a template

I'll bet you changed both:-).

My personal preference for return values is to use the scoping
operator and the name of the type in the class. It just seems
more consistent to me. (I'd also probably find a different name
for the template and the typedef. Using the same name just
looks like a recepe for trouble to me.)

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Patrick Leslie Polzer
Guest





PostPosted: Sat Jan 15, 2005 3:11 am    Post subject: Re: Template class not recognized Reply with quote

Gianni Mariani wrote:
Quote:
You have these:
typedef MacroNode<K,D> MacroNode;
typedef Node<K,D> Node;


Which change the defintion of MacroNode and Node depending on context.

After changing all the instances of Node to Node<K,D> and MacroNode to
MacroNode<K,D>,
I'd like to stay at the core problem first - why do the typedefs for

'Node' work, but not those for 'MacroNode' ?
I can't see either how you relate the new problems (which result out
of the code being written for Node** originally) to this one.


Kind regards,

Leslie

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dave Moore
Guest





PostPosted: Sat Jan 15, 2005 3:34 am    Post subject: Re: Template class not recognized Reply with quote


"Patrick Leslie Polzer" <leslie.polzer (AT) gmx (DOT) net> wrote

Quote:
Hello,

I have a problem with templates which I wasn't able to solve yet.
The error message reads as follows:

btree.hxx:129: error: expected constructor, destructor, or type
conversion before '*' token
btree.hxx:129: error: expected `;' before '*' token

With the offending code line 129 with context being:

template <typename K, typename D
MacroNode* BTree {
/* method impl goes here */
}

But MacroNode<K,D> is well-defined in the included header file
"macronode.hxx":

template <typename K, typename D
class MacroNode
{
/* ... */
}

and MacroNode is typedef'd thus:

template class BTree
{
typedef MacroNode /* ... */
}


If you want to use the MacroNode typedef inside BTree, you need to qualify
(at least) the MacroNode* return value of your insert function. IOW, write

typename BTree<K,D>::MacroNode*
BTree<K,D>::insert(MacroNode* n, Node* m) {/* ... */}

The issue is that you are defining the BTree member function outside the
scope of BTree, so the MacroNode typedef is not available for the return
type. OTOH, it is available for the function parameters, since they are
declared inside the scope of the function, which is already qualified as
being at the scope of BTree.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Patrick Leslie Polzer
Guest





PostPosted: Sun Jan 16, 2005 5:28 am    Post subject: Re: Template class not recognized Reply with quote

[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
Quote:
[lots of good explanation]
I perfectly understand this, yet the changes that are the logical

consequence don't work (here?). Sad I changed it to

template <typename K, typename D>
BTree<K,D>::MacroNode* insert ( MacroNode* n, Node* m )

....to no avail.

I also tried, despite Dave Moore's comment on MacroNode
being used inside as parameter is already in class scope,
this variation:

template <typename K, typename D>
BTree<K,D>::MacroNode* insert ( BTree<K,D>::MacroNode* n, Node* m )

....and, being desperate, just copying your code did not work either.
It's the same error all over again. Why?


Leslie

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
dtmoore
Guest





PostPosted: Mon Jan 17, 2005 12:42 am    Post subject: Re: Template class not recognized Reply with quote

Well, I downloaded the code from the link you provided and tried to
compile it with g++ 3.3.1, and first of all, I got no errors like the
one specified in your OP, but I got several other errors involving
other mistakes and problems in your classes. Checking line 129, I
found the code had a properly templated return value (MacroNode<K,R>*
), so that explains why I didn't see the error you describe. I then
changed the code to match what I put in my earlier reply, and nothing
changed. Perhaps the behavior is different in g++ 3.4.3, but I don't
see why that would be the case unless there were differences elsewhere
in the code. So, I think you should go back and check carefully that
the source of your error is really where you think it is.
HTH,

Dave Moore


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Patrick Leslie Polzer
Guest





PostPosted: Mon Jan 17, 2005 9:06 am    Post subject: Re: Template class not recognized Reply with quote

dtmoore wrote:
Quote:
Well, I downloaded the code from the link you provided and tried to
compile it with g++ 3.3.1, and first of all, I got no errors like the
one specified in your OP, but I got several other errors involving
other mistakes and problems in your classes. Checking line 129, I
found the code had a properly templated return value (MacroNode<K,R>*
), so that explains why I didn't see the error you describe. I then
changed the code to match what I put in my earlier reply, and nothing
changed. Perhaps the behavior is different in g++ 3.4.3,
It is. With gcc version 3.3.2 I get only a warning for the offending

line:

In file included from test.cxx:5:
btree.hxx:177: warning: `BTree<K, D>::MacroNode' is implicitly a typename
btree.hxx:177: warning: implicit typename is deprecated, please see the
documentation for details

I will look into this further and have you updated.

Leslie

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Tue Jan 18, 2005 11:03 pm    Post subject: Re: Template class not recognized Reply with quote

Patrick Leslie Polzer wrote:
Quote:
dtmoore wrote:
Well, I downloaded the code from the link you provided and
tried to compile it with g++ 3.3.1, and first of all, I got
no errors like the one specified in your OP, but I got
several other errors involving other mistakes and problems
in your classes. Checking line 129, I found the code had a
properly templated return value (MacroNode<K,R>* ), so that
explains why I didn't see the error you describe. I then
changed the code to match what I put in my earlier reply,
and nothing changed. Perhaps the behavior is different in
g++ 3.4.3,

It is. With gcc version 3.3.2 I get only a warning for the
offending line:

In file included from test.cxx:5:
btree.hxx:177: warning: `BTree<K, D>::MacroNode' is implicitly a
typename
btree.hxx:177: warning: implicit typename is deprecated, please see
the
documentation for details

The old dependant names problem.

In order to correctly parse C++, it is necessary to know whether
a symbol designates a type or something else. Within a
template, some names depend on instantiation parameters, and the
compiler cannot know whether they designate a type or not until
the template has been instantiated. In order to allow earlier
and better error checking, the standard says that the compiler
should consider that a dependant name is NOT a type unless you
explicitly tell it otherwise. Just pop a "typename" in front of
the qualified expression.

Note that most (all?) pre-standard implementations of templates
did not have earlier and better error checking. In fact, the
ones I know didn't do any more than was necessary to find the
end of the template until instantiation. Thus, they didn't
require typename. A lot of compilers still behave like this
(and of course, any good compiler will have an option to allow
you to compiler the old programs).

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Patrick Leslie Polzer
Guest





PostPosted: Tue Jan 18, 2005 11:11 pm    Post subject: Re: Template class not recognized Reply with quote

Patrick Leslie Polzer wrote:
Quote:
I will look into this further and have you updated.
There you go: http://gcc.gnu.org/ml/gcc/2002-08/msg00010.html


The warning seems to have been introduced in gcc 3.1 and the error
in 3.4 . Hope that helps anyone.


Leslie

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
dtmoore
Guest





PostPosted: Wed Jan 19, 2005 11:54 pm    Post subject: Re: Template class not recognized Reply with quote

*ahem* ... hopefully you noticed that I actually included the typename
keyword in my suggested correction from Jan 14 ... that I why I did not
get the error you mentioned. Anyway, does your code now compile and
run properly?

Dave Moore


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Patrick Leslie Polzer
Guest





PostPosted: Thu Jan 20, 2005 6:21 pm    Post subject: Re: Template class not recognized Reply with quote

dtmoore wrote:
Quote:
*ahem* ... hopefully you noticed that I actually included the typename
keyword in my suggested correction from Jan 14 ... that I why I did not
get the error you mentioned.
Yes - I did; why it did not work at this time is a conundrum to me Sad

I cannot remember the problems I had back then, but they must have
had some other cause. But now at least I understand why that 'typename'
is needed, so the whole thing was not in vain.

Quote:
Anyway, does your code now compile and run properly?
Yes, many thanks for your help!



Leslie

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.