wierd template

Y

yilled_fred

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!.
 
G

Gianni Mariani

yilled_fred said:
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>

#include <cstdio>
#include said:
template <typename T>
class baseclass{
public:
baseclass(){}
template <typename R>
void bar(){
R i=5;
cout<<i<<endl;
}
};
^^
no semicolon needed here
template<typename S>
class derivedclass:public baseclass<S>{
public:
derivedclass(){}
void foo(){
bar<S>(); <---- error here
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

^^
no semicolon needed here
 
V

Victor Bazarov

Gianni said:
#include <cstdio>
#include <cstdlib>

That doesn't matter.
template <typename T>
class baseclass{
public:
baseclass(){}
template <typename R>
void bar(){
R i=5;
cout<<i<<endl;
}
};

^^
no semicolon needed here
WHAT????
template<typename S>
class derivedclass:public baseclass<S>{
public:
derivedclass(){}
void foo(){
bar<S>(); <---- error here

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


^^
no semicolon needed here
WHAT???????????????

[...]


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
 
C

Clark S. Cox III

#include <cstdio>
#include <cstdlib>

Generally good advice, but note that the previous version wasn't
"wrong", it was only depricated.
^^
no semicolon needed here

It certainly is needed.
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


^^
no semicolon needed here

Again, yes it is needed.
 
J

jon

i think that would still cause an error. try:

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

jon hanson
 
G

Gianni Mariani

Victor said:
OOPS
....



WHAT???????????????
OOPS #2
....



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

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

Just as well others are sober !

Thanks
 
Y

yilledfred

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.
 
J

jon

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
 

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
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top