Afficher un message
Vieux 06/12/2007, 17h30   #3
Abhishek Padmanabh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Am I the only one mad here?

On Dec 6, 8:52 pm, Darko <darko.maksimo...@gmail.com> 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.
> * 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? Personally, it broke my C++
> confidence.


As a resolution, you may want to put using A::f; in class B, the
derived class. This brings the base class overload in the set of
overloads that b.f() will look for to find a match. And it will find
it.
  Réponse avec citation
 
Page generated in 0,06521 seconds with 9 queries