Re: Am I the only one mad here?
Darko wrote:
> The following code doesn't work:
>
> 01: class A {
> 02: public:
> 03: bool f( int x )
> 04: {
> 05: return x>1;
> 06: }
> 07: };
> 08:
> 09: class B: public A {
> 10: public:
> 11: bool f()
> 12: {
> 13: return f(3) ;
> 14: }
> 15: };
> 16:
> 17: int main()
> 18: {}
>
> Compiler reports the following error:
> g++ proba.cpp -o proba
> proba.cpp: In member function `bool B::f()':
> proba.cpp:13: error: no matching function for call to `B::f(int)'
> proba.cpp:12: note: candidates are: bool B::f()
> make: *** [proba] Error 1
>
> Since I suppose you are going to propose putting A::f(3), I can tell
> you I've already tried to do that and it works. But:
> * Why would I have to do that!? The methods are perfectly distinct
> having different arguments.
Read about "overloading" versus "hiding".
> * If I do that, then the following code still doesnt work:
>
> 01: class A {
> 02: public:
> 03: bool f( int x )
> 04: {
> 05: return x>1;
> 06: }
> 07: };
> 08:
> 09: class B: public A {
> 10: public:
> 11: bool f()
> 12: {
> 13: return A::f(3) ;
> 14: }
> 15: };
> 16:
> 17: int main()
> 18: {
> 19: B b;
> 20: b.f( 4 );
> 21: }
> giving the following error:
>
> proba.cpp: In function `int main()':
> proba.cpp:20: error: no matching function for call to `B::f(int)'
> proba.cpp:12: note: candidates are: bool B::f()
> make: *** [proba] Error 1
>
> Now what do you think about that?
Nothing in particular. It's the error in your code. You can fix
it by doing
b.A::f(4);
> Personally, it broke my C++
> confidence.
Was there confidence to begin with?
As soon as you understand the difference between "overloading",
"hiding", and "overriding", you'll know what to do. You also might
want to learn about "name lookup", although it's not really a simple
topic.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
|