Error I can't understand

  • Thread starter Carlos Martinez Garcia
  • Start date
C

Carlos Martinez Garcia

Hi all:

I have a problem compiling a file and I can't understand why.

Here is my code:

class Base {
public:
virtual void foo()=0;
virtual void foo(int i) {
int j=i;
}
virtual void foo(int i, int i2, int i3) {
int j=i;
}
};

class Derived :public Base {
public:
void foo() {}
void foo(int i,int i2,int i3) {
int j=i+5;
}
};

int main() {
Derived d;

d.foo(15);

return 0;
}

The compiler's output:

main.cc: En function `int main()':
main.cc:23: error: no matching function for call to `Derived::foo(int)'
main.cc:14: error: candidates are: virtual void Derived::foo()
main.cc:15: error: virtual void Derived::foo(int, int, int)


Why compiler doesn't see foo(int)?
It must be Base::foo(int). Isn't it?

If I change foo(int) to foo2(int), there's no errors. Why?

Thanks in advance
 
U

upashu1

Because by overloading the function of same name in derived class, you
hide the definitions of functions in base class. Overlaoding between
different class scope is not permitted. You may use "using" keyword to
unhide the definitions of base class.
class Derived :public Base {
public:
using Base::foo;
void foo() {}
void foo(int i,int i2,int i3) {
int j=i+5;
}

};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top