When trying to compile the program class A {public: void a() {};};class B: public A {public: void a(int n) {};};int main(void){ B b; b.a(); return 0;} I get the error message: test.cc: In function `int main()': test.cc:15: no matching function for call to `B::a ()' test.cc:8: candidates are: void B::a(int) I believe the code is correct (so do all my colleagues). Changing the method name to 'b()' (or anything else) fixes the problem. The code snippet does not exhibit good programming style, but nonetheless it seems to be correct. C++ books in my reach gave no help on the subject. Release: gcc version 2.95.2 19991024 (release) Environment: SuSe Linux 7.2 on IBM Thinkpad A21p How-To-Repeat: Compile code with g++
State-Changed-From-To: open->closed State-Changed-Why: Not a bug.
From: "Artem Khodush" <artem@duma.gov.ru> To: <volker.reichenberger@iwr.uni-heidelberg.de> Cc: <gcc-gnats@gcc.gnu.org>, <gcc-bugs@gcc.gnu.org> Subject: Re: c++/3400: doesn't compile simple hierarchy with overlaoding Date: Tue, 26 Jun 2001 11:56:20 +0400 > > class A {public: void a() {};};class B: public A {public: void a(int n) {};};int main(void){ B b; b.a(); return 0;} > > I get the error message: > > test.cc: In function `int main()': > test.cc:15: no matching function for call to `B::a ()' > test.cc:8: candidates are: void B::a(int) > > I believe the code is correct (so do all my colleagues). Nope. See the quote from the c++ standard below. In GCC 3.0, you can fix the code by putting using A::a(); into the class B. 13.2 Declaration matching [over.dcl] 1 Two function declarations of the same name refer to the same function if they are in the same scope and have equivalent parameter declara- tions (_over.load_). A function member of a derived class is not in the same scope as a function member of the same name in a base class. [Example: class B { public: int f(int); }; class D : public B { public: int f(char*); }; Here D::f(char*) hides B::f(int) rather than overloading it. void h(D* pd) { pd->f(1); // error: // D::f(char*) hides B::f(int) pd->B::f(1); // ok pd->f("Ben"); // ok, calls D::f } --end example]