Connecting Tech Pros Worldwide Forums | Help | Site Map

Error I can't understand

Carlos Martinez Garcia
Guest
 
Posts: n/a
#1: Jul 23 '05
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

Tim Love
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Error I can't understand


Carlos Martinez Garcia <cmg250@tid.es> writes:
[color=blue]
>Hi all:[/color]
[color=blue]
>I have a problem compiling a file and I can't understand why.[/color]
See
http://www.parashift.com/c++-faq-lit....html#faq-23.7
or maybe
http://www-h.eng.cam.ac.uk/help/tpl/...ml#Inheritance
upashu1@rediffmail.com
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Error I can't understand


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;
}

};

Closed Thread