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