Connecting Tech Pros Worldwide Forums | Help | Site Map

wierd template

yilled_fred
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi can anyone tell me why the following code cannot/doesn't compile?
I get the following error while compiling (g++ -c ):

"In member function `void derivedclass<S>::foo()':
test.h:20 error: parse error before `;' token

Here is the code, am I missing something?

#include <stdio.h>
#include <stdlib.h>

template <typename T>
class baseclass{
public:
baseclass(){}
template <typename R>
void bar(){
R i=5;
cout<<i<<endl;
}
};

template<typename S>
class derivedclass:public baseclass<S>{
public:
derivedclass(){}
void foo(){
bar<S>(); <---- error here
}
};

template class derivedclass<int>;


thanks!.

Gianni Mariani
Guest
 
Posts: n/a
#2: Jul 23 '05

re: wierd template


yilled_fred wrote:[color=blue]
> Hi can anyone tell me why the following code cannot/doesn't compile?
> I get the following error while compiling (g++ -c ):
>
> "In member function `void derivedclass<S>::foo()':
> test.h:20 error: parse error before `;' token
>
> Here is the code, am I missing something?
>
> #include <stdio.h>
> #include <stdlib.h>[/color]

#include <cstdio>
#include <cstdlib>
[color=blue]
>
> template <typename T>
> class baseclass{
> public:
> baseclass(){}
> template <typename R>
> void bar(){
> R i=5;
> cout<<i<<endl;
> }
> };[/color]
^^
no semicolon needed here
[color=blue]
>
> template<typename S>
> class derivedclass:public baseclass<S>{
> public:
> derivedclass(){}
> void foo(){
> bar<S>(); <---- error here[/color]
The compiler can't tell if there is a bar() in the derived class because
it depends on S. For example, a specialization of baseclass<S> may
eliminate the bar() method.

A number of different solutions - replace bar<S>() with:

this->bar<S>()
or
baseclass<S>::bar<S>()
or
derivedclass::bar<S>()

or have a

using baseclass<S>::bar; in a scope within the class (not sure about
this syntax for a template).
[color=blue]
> }
> };[/color]
^^
no semicolon needed here
[color=blue]
>
> template class derivedclass<int>;
>
>
> thanks!.[/color]
Victor Bazarov
Guest
 
Posts: n/a
#3: Jul 23 '05

re: wierd template


Gianni Mariani wrote:[color=blue]
> yilled_fred wrote:
>[color=green]
>> Hi can anyone tell me why the following code cannot/doesn't compile?
>> I get the following error while compiling (g++ -c ):
>>
>> "In member function `void derivedclass<S>::foo()':
>> test.h:20 error: parse error before `;' token
>>
>> Here is the code, am I missing something?
>>
>> #include <stdio.h>
>> #include <stdlib.h>[/color]
>
>
> #include <cstdio>
> #include <cstdlib>[/color]

That doesn't matter.
[color=blue]
>[color=green]
>>
>> template <typename T>
>> class baseclass{
>> public:
>> baseclass(){}
>> template <typename R>
>> void bar(){
>> R i=5;
>> cout<<i<<endl;
>> }
>> };[/color]
>
> ^^
> no semicolon needed here[/color]

WHAT????
[color=blue][color=green]
>> template<typename S>
>> class derivedclass:public baseclass<S>{
>> public:
>> derivedclass(){}
>> void foo(){
>> bar<S>(); <---- error here[/color]
>
> The compiler can't tell if there is a bar() in the derived class because
> it depends on S. For example, a specialization of baseclass<S> may
> eliminate the bar() method.
>
> A number of different solutions - replace bar<S>() with:
>
> this->bar<S>()
> or
> baseclass<S>::bar<S>()
> or
> derivedclass::bar<S>()
>
> or have a
>
> using baseclass<S>::bar; in a scope within the class (not sure about
> this syntax for a template).
>[color=green]
>> }
>> };[/color]
>
> ^^
> no semicolon needed here[/color]

WHAT???????????????
[color=blue]
> [...][/color]


All good suggestions about this->bar and baseclass<S>::bar, but, Gianni,
what's with the semicolon notes?

Paraphrasing somebody else here, who are you and what have you done to
the real Gianni Mariani?

V
Clark S. Cox III
Guest
 
Posts: n/a
#4: Jul 23 '05

re: wierd template


On 2005-03-11 09:43:23 -0500, Gianni Mariani <gi2nospam@mariani.ws> said:
[color=blue]
> yilled_fred wrote:[color=green]
>> Hi can anyone tell me why the following code cannot/doesn't compile?
>> I get the following error while compiling (g++ -c ):
>>
>> "In member function `void derivedclass<S>::foo()':
>> test.h:20 error: parse error before `;' token
>>
>> Here is the code, am I missing something?
>>
>> #include <stdio.h>
>> #include <stdlib.h>[/color]
>
> #include <cstdio>
> #include <cstdlib>[/color]

Generally good advice, but note that the previous version wasn't
"wrong", it was only depricated.
[color=blue]
>[color=green]
>>
>> template <typename T>
>> class baseclass{
>> public:
>> baseclass(){}
>> template <typename R>
>> void bar(){
>> R i=5;
>> cout<<i<<endl;
>> }
>> };[/color]
> ^^
> no semicolon needed here[/color]

It certainly is needed.
[color=blue]
>[color=green]
>>
>> template<typename S>
>> class derivedclass:public baseclass<S>{
>> public:
>> derivedclass(){}
>> void foo(){
>> bar<S>(); <---- error here[/color]
> The compiler can't tell if there is a bar() in the derived class
> because it depends on S. For example, a specialization of baseclass<S>
> may eliminate the bar() method.
>
> A number of different solutions - replace bar<S>() with:
>
> this->bar<S>()
> or
> baseclass<S>::bar<S>()
> or
> derivedclass::bar<S>()
>
> or have a
>
> using baseclass<S>::bar; in a scope within the class (not sure about
> this syntax for a template).
>[color=green]
>> }
>> };[/color]
> ^^
> no semicolon needed here[/color]

Again, yes it is needed.

--
Clark S. Cox, III
clarkcox3@gmail.com

jon
Guest
 
Posts: n/a
#5: Jul 23 '05

re: wierd template


i think that would still cause an error. try:

void foo(){
this->template bar<S>();
}

jon hanson


Gianni Mariani wrote:[color=blue]
> yilled_fred wrote:[/color]
....[color=blue]
>
> A number of different solutions - replace bar<S>() with:
>
> this->bar<S>()
> or
> baseclass<S>::bar<S>()
> or
> derivedclass::bar<S>()
>
> or have a
>
> using baseclass<S>::bar; in a scope within the class (not sure about
> this syntax for a template).
>[color=green]
> > }
> > };[/color]
> ^^
> no semicolon needed here
>[color=green]
> >
> > template class derivedclass<int>;
> >
> >
> > thanks!.[/color][/color]

Gianni Mariani
Guest
 
Posts: n/a
#6: Jul 23 '05

re: wierd template


Victor Bazarov wrote:[color=blue]
> Gianni Mariani wrote:
>[color=green]
>> yilled_fred wrote:[/color][/color]
....[color=blue][color=green][color=darkred]
>>> };[/color]
>>
>>
>> ^^
>> no semicolon needed here[/color]
>
>
> WHAT????
>[/color]
OOPS
....
[color=blue][color=green][color=darkred]
>>> };[/color]
>>
>>
>> ^^
>> no semicolon needed here[/color]
>
>
> WHAT???????????????
>[/color]
OOPS #2
....[color=blue][color=green]
>> [...][/color]
>
>
>
> All good suggestions about this->bar and baseclass<S>::bar, but, Gianni,
> what's with the semicolon notes?[/color]

Yikes .... I didn't drink too much this morning - honest !
[color=blue]
>
> Paraphrasing somebody else here, who are you and what have you done to
> the real Gianni Mariani?[/color]

Just as well others are sober !

Thanks

yilledfred@gmail.com
Guest
 
Posts: n/a
#7: Jul 23 '05

re: wierd template


Thanks a lot for your answers, the last one did work!

Note:

I believe the first answer (haven't tried it) is not correct because
the compiler won't generate the code for the bar<S>() template function
either! But why does the following code does? Is that a kind of
"explicit template member function instanciation"?

regards,

f.

jon wrote:[color=blue]
>
> void foo(){
> this->template bar<S>();
> }
>
> jon hanson
>[/color]

jon
Guest
 
Posts: n/a
#8: Jul 23 '05

re: wierd template


If you're referring to my answer, well as gianni's first post stated;

"The compiler can't tell if there is a bar() in the derived class
because
it depends on S. For example, a specialization of baseclass<S> may
eliminate the bar() method."

More so, it can't know what bar is. This means that in the following
the call to bar:
this->bar<S>()
the compiler doesn't know that bar is templated and parses it as
something like:
((this->bar) < S) > ()
hence the parse error.

The template keyword indicates to the compiler that bar is templated
and allows it to parse it correctly. You can also use this for the ::
and . operators.

Check out "C++ Templates" by Vandevoorde & Josuttis for more info (page
132) - an excellent intro and reference for templates.

jon

Closed Thread