0

I'm writing some c++ code which looks like:

class Base {
public:
  virtual ~Base() {}
  void foo(int a) {}
  virtual void foo(int a, int b) = 0;
};

class Derived: public Base {
public:
  virtual void foo(int a, int b) {}
};

int main()
{
  Derived o;
  o.foo(1);

  return 0;
}

This produces the following error:

candidate expects 2 arguments, 1 provided

At first I thought of a compilator bug, but after trying with different ones and always getting the same result, I realize it must be part of the standard. Can somebody please point out the reason for that error?

Gianluca
  • 805
  • 1
  • 10
  • 20

1 Answers1

1

This is because of name hiding.

Declaring a function in a derived class with the same name as those in the base class hides the ones in the base class.

If you also want to be able to call void foo(int) on Derived, put in a using-declaration:

class Derived: public Base {
public:
  using Base::foo; //here
  virtual void foo(int a, int b) {}
};
TartanLlama
  • 63,752
  • 13
  • 157
  • 193