1

I am typing the following code and I am getting the following error at line-1
[Error] no matching function for call to 'int_adder::add()

#include <iostream> 
using namespace std; 

class adder{ 
  public: 
  void add(){ cout <<"adder::add() "; } 
}; 

class int_adder : public adder{ 
  public: 
  int add(int a, int b){
    return (a + b); 
  }
};

int main(){ 
  int_adder ia; 
  ia.add(); //LINE-1 
  cout << ia.add(10, 20); //LINE-2 
  return 0; 
}
Saptarshi Dey
  • 125
  • 1
  • 7
  • 1
    You function in `int_adder` is shadowing the one in the base class. To pull it into the overload set you can add `using adder::add;` in the definition for `int_adder`. – super Feb 18 '21 at 10:58
  • 3
    `int_adder::add()` has a different signature (argument types and return type) than `adder::add()`. According to the standard, `int_adder::add()` has the effect of HIDING `adder::add()`, not overriding it or overloading it. This is informally described as "the hiding rule". – Peter Feb 18 '21 at 11:01
  • 1
    does this answer your question ? https://stackoverflow.com/questions/18688799/what-is-the-hiding-rule-in-c – Arsenic Feb 18 '21 at 11:03
  • 1
    Does this answer your question? [What is the hiding rule in c++?](https://stackoverflow.com/questions/18688799/what-is-the-hiding-rule-in-c) – luk32 Feb 19 '21 at 14:13

3 Answers3

1

As pointed by others in the comment, I have corrected it:-

#include <iostream> 
using namespace std; 

class adder{ 
  public: 
    void add(){ cout <<"adder::add() "; } 
}; 

class int_adder : public adder{ 
  public: 
     int add(int a, int b){
          return (a + b); 
     }
};

int main(){ 
  int_adder ia; 
  ia.adder::add(); //LINE-1 
  cout << ia.add(10, 20); //LINE-2 
  return 0; 
}

The statement adder::add() will overide the function add() present in int_adder.

Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24
1

Can't fing exact dupe, but you can make overloads from base class visible by using using directive, example:

#include <iostream> 
using namespace std; 

class adder{ 
  public: 
  void add(){ cout <<"adder::add() "; } 
}; 

class int_adder : public adder{ 
  public: 
  using adder::add; // expose base class overload as our own

  int add(int a, int b){
    return (a + b); 
  }
};

int main(){ 
  int_adder ia; 
  ia.add(); //LINE-1 
  ia.adder::add(); // explicit name also works
  cout << ia.add(10, 20); //LINE-2 
  return 0; 
}

As the other answer mentions using base class name scope also works. It all depends on your needs and class design.

Basically defining an overload in a derived class prevents implicit method look up from matching base class overloads, so you have to be explicit about it in one way or another.

luk32
  • 15,812
  • 38
  • 62
0

You have totally two different versions of add in the first place. You are not overriding, you are overloading. You are just providing a new add function that has nothing to do with the other add function of the parent.

So first of all, do you want to override or overload?

Overriding the parent add would like the following:

#include <iostream> 
using namespace std; 

class adder{ 
  public: 
    void add(){ cout <<"adder::add() "; } 
}; 

class int_adder : public adder{ 
  public: 
     int add() override {cout <<"int_adder ::add() ";}; 
     int add(int a, int b){
          return (a + b); 
     }
};

int main(){ 
  int_adder ia; 
  ia.adder::add(); //LINE-1 <- would work displays adder::add()'s message
  cout << ia.add(10, 20); //LINE-2 
  
  adder ia = int_adder{}; // now this is interesting
  ia.add(); // would work displays adder::add()'s message - cause you did not ask for virtuality

  return 0; 
}
Oussama Ben Ghorbel
  • 2,132
  • 4
  • 17
  • 34