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!. 7 1248
yilled_fred wrote: 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 <cstdlib> 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
using baseclass<S>::bar; in a scope within the class (not sure about
this syntax for a template).
} };
^^
no semicolon needed here template class derivedclass<int>;
thanks!.
Gianni Mariani wrote: yilled_fred wrote:
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 <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
using baseclass<S>::bar; in a scope within the class (not sure about this syntax for a template).
} };
^^ 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
On 2005-03-11 09:43:23 -0500, Gianni Mariani <gi*******@mariani.ws> said: yilled_fred wrote: 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 <cstdlib>
Generally good advice, but note that the previous version wasn't
"wrong", it was only depricated. template <typename T> class baseclass{ public: baseclass(){} template <typename R> void bar(){ R i=5; cout<<i<<endl; } }; ^^ no semicolon needed here
It certainly is needed. 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
using baseclass<S>::bar; in a scope within the class (not sure about this syntax for a template).
} }; ^^ no semicolon needed here
Again, yes it is needed.
--
Clark S. Cox, III cl*******@gmail.com
i think that would still cause an error. try:
void foo(){
this->template bar<S>();
}
jon hanson
Gianni Mariani wrote: yilled_fred wrote:
.... 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).
} }; ^^ no semicolon needed here
template class derivedclass<int>;
thanks!.
Victor Bazarov wrote: Gianni Mariani wrote:
yilled_fred wrote:
.... };
^^ no semicolon needed here
WHAT????
OOPS
.... };
^^ no semicolon needed here
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
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: void foo(){ this->template bar<S>(); }
jon hanson
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: vool |
last post by:
Hi all,
I've got a really wierd problem here.
When I access a web page that adds information to a database on a PC running
XP Pro it works as it should.
When I access the same page from a...
|
by: Markus Fischer |
last post by:
Hi,
I'm experiencing a wierd problem with IE 6 in Windows with two
_slightly_ different Version.
Give the following HTMl-Code, ideally the output of offsetTop should be
"105"; a few pixel...
|
by: Qun Cao |
last post by:
Hello,
I am just starting to play threading in python, here is a really
interesting problem I am very curious about:
"
import thread
def main():
thread.start_new(test.())
def test():
|
by: Protoman |
last post by:
I'm trying to print a complex number and i get this as output: Phi:
(1.618034,0.000000).
What does it mean? How do I get it to print normally? Here's the code:
----------fib.hpp-------------...
|
by: Sathyaish |
last post by:
A practice excercise from K&R. Kindly read the comments within the
program. I'd be very grateful to people who helped. Why is it that I
get the wierd face-like characters on the screen instead of...
|
by: Michael |
last post by:
Hi,
I found a wierd problem in DataGrid.
If I set DataGrid's DataSource to empDataSet1 at designtime,
then I can never change its DataSource at runtime, e.g., in the
Button1_Click event...
|
by: Tom |
last post by:
OK, here's a wierd one... I have a listbox, which I fill with strings (in my
case, file names). I normally load this via a loop, adding each item to the
list box in the loop. I put lb.BeginUpdate...
|
by: Tom |
last post by:
We are experiencing some wierd debugging behavior. What happens is
that, during debugging with VS 2003, the debugger seems to 'skip'
statements that are associated with database operations. For...
|
by: desktop |
last post by:
I am confused about the use of the template parameter "E" in the below
class. Since when is it allowed to use these parameters like "E(1)" and
what does it mean (where can I read more about this...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |