This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Inheritance/overloading problem
- To: Dean Wakerley <dean at housefloors dot co dot uk>
- Subject: Re: Inheritance/overloading problem
- From: Kevin Handy <kth at srv dot net>
- Date: Tue, 04 Sep 2001 09:31:45 -0600
- CC: gcc at gcc dot gnu dot org
- References: <001501c13551$8b5dfbe0$0b0b11ac@rhf.net>
Dean Wakerley wrote:
>
> I'm getting this error message when compiling the following code.
>
> test.cc: In function `int main()':
> test.cc:31: no matching function for call to `top_c::func (char)'
> test.cc:20: candidates are: void top_c::func()
>
> // ----- start of code ---------
> #include <iostream>
>
> class base_c
> {
> public:
> virtual void func()=0;
> virtual void func(char)=0;
> };
>
> class middle_c
Item 1: You are NOT inheriting from base_c here.
You should add a ' : public base_c' here.
> {
> public:
> void func() { cout << "middle_c::func()" << endl; };
> void func(char) { cout << "middle_c::func(char)" << endl; };
> };
>
> class top_c
Item 2: You are NOT inheriting from middle_c here.
You should add a ' : public middle_c' here.
> {
> public:
> void func() { cout << "top_c::func()" << endl; };
Item 3: When you overide a function by name, you overide ALL of the
functions with that name. Part of the C++ language definition.
Item 4: Since you haven't inherited from previous classes, you won't
be able to see functions in their classes either.
> /* void func(char);
> * use function inherited from middle_c
> */
> };
>
> int
> main(/*...*/)
> {
> top_c t;
> t.func(); // works
> t.func('x'); // problem: no matching function???
You could try
t.middle_c::func('x');
> return 0;
> }
>
> // ----- end of code ---------
>
> Is this a language problem or a compiler problem?, and how do I get around
> it?
I'd say it's largely a coding problem.