2
class A
{
A() {};
virtual ~A() {};
virtual void Start() {};
virtual void Start(float a) {};
};

class B : public A
{ };

class C : public A
{
virtual void Start(float a) {};
}


...
B BObj;
BObj.Start(); // -> fine, no complain from g++
...

...
C CObj;
CObj.Start(); // -> not fine -> error: no matching function for call to ‘C::Start()’
...

I suspect that the problem comes from that both virtual functions have the same name, but different parameter signature. What I would like to know is that this is a g++-specific error message, how it is implemented the vtable, or it is an error based on the C++ standard.

user229044
  • 232,980
  • 40
  • 330
  • 338
kecsap
  • 350
  • 3
  • 8
  • 2
    There's something missing above; the class routines are all private by default, so the code as you have written it above would emit different errors than the one you are posting... – fbrereto Feb 03 '11 at 20:17
  • As @fbrereto points out the visibility of your methods is limited to the class A neither B nor C inherit them. – josefx Feb 03 '11 at 20:21
  • I think the author just threw together some code quickly. The ... in their current locations would also elicit compiler errors – Foo Bah Feb 03 '11 at 20:25
  • I suspect this was a design decision: lets suppose that float argument in the second function were optional [i.e. virtual void Start(float a = 3) {};] Then the definitions would be ambiguous – Foo Bah Feb 03 '11 at 20:29
  • All the functions in A are private, so the compiler don't find them. – Murilo Vasconcelos Feb 03 '11 at 20:39
  • Thanks for correcting my sample code. I really was very tired when I wrote. – kecsap Feb 04 '11 at 08:13

5 Answers5

7

Overloading function hides all other Start functions. To use them add using A::Start:

class C : public A
{
public:
using A::Start;
virtual void Start(float a) {};
}

Also make Start public in A too.

Edit: Here you can find why derived class hides base class functions.

Community
  • 1
  • 1
UmmaGumma
  • 5,633
  • 1
  • 31
  • 45
0

The overload for Start in C hides all of the overloaded Start versions from A. If you did not try to overload Start in A [i.e. Start0(), Start1(float)] you wouldnt see this problem.

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
0

You seek to call Start() on a CObj. But there is no such function because the only function defined is the overloaded Start(float a) which takes in a float parameter.

Exactly as the compiler says.

If a Start() function is deflared in the C class, calling this function should be fine. It can be declared as virtual and ned not be defined/implemented.

Hope this helps.

Igbanam
  • 5,904
  • 5
  • 44
  • 68
0
class A
{
public:
    A() {}
    virtual ~A() {}

    virtual void Start() {}
    virtual void Start(float a) {}
};

class B : public A
{
};

class C : public A
{
public:
    using A::Start;
    virtual void Start(float a) {}
};

int main ()
{
    B BObj;
    BObj.Start();

    C CObj;
    CObj.Start ();
}
0

when you overload a function in another class, for ever call overloaded function if else you call it. for example

class A()
{
    void start();

};
class B:public A
{
   void start();
   void Start();{A::start}//this function call it's father function
}
void main()
{
  A a;
  B b;
  a.start();//call own function,start() in A.
  b.start();//call is own function,start() in B.
  b.Start();//call start() in A.
}
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117