Bug 3400 - doesn't compile simple hierarchy with overlaoding
Summary: doesn't compile simple hierarchy with overlaoding
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 2.95.2
: P2 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2001-06-25 04:16 UTC by volker.reichenberger
Modified: 2003-07-25 17:33 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description volker.reichenberger 2001-06-25 04:16:01 UTC
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++
Comment 1 Kriang Lerdsuwanakij 2001-06-26 05:26:16 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Not a bug.
Comment 2 artem 2001-06-26 11:56:20 UTC
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]