c++ - Compiler can't find base class method when called from derived, and the derived defines same named method with additional parameter -
here's link ideone simple code paste: http://ideone.com/bbck3b .
the base class has paramtereless function, whereas derived has 1 parameter. public.
why compiler fails find a::foo() when called instance of b?
the code:
#include <iostream> using namespace std; class { public: virtual void foo() { cout << "a::foo" << endl; } }; class b : public { public: void foo(int param) { cout << "b::foo " << param << endl; } }; int main() { b b; b.foo(); }
the compiler error:
prog.cpp: in function ‘int main()’: prog.cpp:25:11: error: no matching function call ‘b::foo()’ b.foo(); ^ prog.cpp:25:11: note: candidate is: prog.cpp:16:10: note: void b::foo(int) void foo(int param) ^ prog.cpp:16:10: note: candidate expects 1 argument, 0 provided
this standard c++ behaviour: base class method hidden derived-class method of same name, regardless of arguments , qualifiers. if want avoid this, have explicitly make base-class method(s) available:
class b : public { public: void foo(int param) // hides a::foo() { cout << "b::foo " << param << endl; } using a::foo; // make a::foo() visible again };
Comments
Post a Comment