Topic: Inheritance/overloading problem (correct post: ignore previous post)


Author: "Peter Grigor" <p_grigor@yahoo.com>
Date: Sun, 9 Sep 2001 00:43:31 GMT
Raw View
The class base_c is an abstract class...therefore any classes derived from
base_c must implement the pure virtual functions func() and func(char) or
they too will become abstract classes.

The problem seems to be in the compiler not giving you the proper error
message...by your definition of top_c you really should be implementing both
versions of func(), but you're not. **Therefore top_c should become an
abstract class, and you shouldn't be able to create an object of type
top_c.**

Notice, that if you delete top_c's redifinition of func(void) your code will
compile. Also if you implement both func() and func(char) in top_c, then
comment out the definitions in middle_c your code will compile.

Peter
<^_^>


"Dean Wakerley" <dean@housefloors.co.uk> wrote in message
news:9n2rdt$vvv$1@newsg4.svr.pol.co.uk...
> Apologies for the omission in previous posting, it kinda got left out when
I
> "Noddy"fied the code out of a larger project.
>
> 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: public base_c
> {
> public:
>   void func() { cout << "middle_c::func()" << endl; };
>   void func(char) { cout << "middle_c::func(char)" << endl; };
> };
>
> class top_c: public middle_c
> {
> public:
>   void func() { cout << "top_c::func()" << endl; };
>   /* void func(char);
>    * use function inherited from middle_c
>    */
> };
>
> int
> main(/*...*/)
> {
>   top_c t;
>   t.func();    // works
>   t.func('x'); // problem: no matching function???
>   return 0;
> }
>
> // ----- end of code ---------
>
> Is this a language problem or a compiler problem?, and how do I get around
> it?
>
> TIA,
> Dean
>
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Terion" <terion@hotmail.com>
Date: Sun, 9 Sep 2001 13:43:51 GMT
Raw View
"Dean Wakerley" <dean@housefloors.co.uk> schreef in bericht
news:9n2rdt$vvv$1@newsg4.svr.pol.co.uk...
> Apologies for the omission in previous posting, it kinda got left out when
I
> "Noddy"fied the code out of a larger project.
>
> 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: public base_c
> {
> public:
>   void func() { cout << "middle_c::func()" << endl; };
>   void func(char) { cout << "middle_c::func(char)" << endl; };
> };
>
> class top_c: public middle_c
> {
> public:
>   void func() { cout << "top_c::func()" << endl; };
>   /* void func(char);
>    * use function inherited from middle_c
>    */
> };
>
> int
> main(/*...*/)
> {
>   top_c t;
>   t.func();    // works
>   t.func('x'); // problem: no matching function???
>   return 0;
> }
>
> // ----- end of code ---------
>
> Is this a language problem or a compiler problem?, and how do I get around
> it?

No, it's a problem with your program. The function middle_c::func(char) is
hidden in class top_c. To make it visble again you should add using
middle_c::func; to class top_c.

Terion.

>
> TIA,
> Dean
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: James Dennett <jdennett@acm.org>
Date: Mon, 10 Sep 2001 16:45:06 GMT
Raw View
Peter Grigor wrote:
> The class base_c is an abstract class...therefore any classes
> derived from base_c must implement the pure virtual functions
> func() and func(char) or they too will become abstract classes.

So far, true.

>
> The problem seems to be in the compiler not giving you the
> proper error message...by your definition of top_c you really
> should be implementing both versions of func(), but you're not.

Wrong.  top_c inherits definitions of both from middle_c.

> **Therefore top_c should become an
> abstract class, and you shouldn't be able to create an object of type
> top_c.**

Pureness only requires that the member function be overridden by
one of the derived classes.

>
> Notice, that if you delete top_c's redifinition of func(void)
> your code will compile.


True, that avoids hiding a base class function.  This has nothing
to do with the existence of pure virtual functions.

> Also if you implement both func() and func(char) in top_c, then
> comment out the definitions in middle_c your code will compile.

Of course.  Or add a using declaration to top_c, as others have
pointed out.  This isn't a language or compiler problem, just a
common misunderstanding.  (So, in that sense I guess it is a
language problem; the language is hard to teach.)

-- James Dennett


>
> "Dean Wakerley" <dean@housefloors.co.uk> wrote in message
> news:9n2rdt$vvv$1@newsg4.svr.pol.co.uk...
>
>>Apologies for the omission in previous posting, it kinda got left out when
>>
> I
>
>>"Noddy"fied the code out of a larger project.
>>
>>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: public base_c
>>{
>>public:
>>  void func() { cout << "middle_c::func()" << endl; };
>>  void func(char) { cout << "middle_c::func(char)" << endl; };
>>};
>>
>>class top_c: public middle_c
>>{
>>public:
>>  void func() { cout << "top_c::func()" << endl; };
>>  /* void func(char);
>>   * use function inherited from middle_c
>>   */
>>};
>>
>>int
>>main(/*...*/)
>>{
>>  top_c t;
>>  t.func();    // works
>>  t.func('x'); // problem: no matching function???
>>  return 0;
>>}
>>
>>// ----- end of code ---------
>>
>>Is this a language problem or a compiler problem?, and how do I get around
>>it?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Mon, 10 Sep 2001 11:54:19 CST
Raw View
In article <RSvm7.2494$uf3.681057@typhoon1.gnilink.net>, Peter Grigor
<p_grigor@yahoo.com> writes
>The class base_c is an abstract class...therefore any classes derived from
>base_c must implement the pure virtual functions func() and func(char) or
>they too will become abstract classes.

Not really. Any class which has a direct (non-virtual) base that is
abstract must implement all the pure virtual functions declared in that
base or, in turn, be an abstract base class.

>
>The problem seems to be in the compiler not giving you the proper error
>message...by your definition of top_c you really should be implementing both
>versions of func(), but you're not. **Therefore top_c should become an
>abstract class, and you shouldn't be able to create an object of type
>top_c.**

Sorry, but that is simply untrue. top_c inherits from a concrete class
and has no pure virtual functions of its own and so is a concrete class.

>
>Notice, that if you delete top_c's redifinition of func(void) your code will
>compile. Also if you implement both func() and func(char) in top_c, then
>comment out the definitions in middle_c your code will compile.

Which seems to completely contrary to your explanation.

The problem had nothing to do with the functions being virtual. Any time
you declare an overrider for an inherited member function that is part
of an overload set, that declaration hides all other members of the
overload set unless you take explicit action such as a using
declaration.


Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Peter Grigor" <p_grigor@yahoo.com>
Date: Mon, 10 Sep 2001 20:39:28 GMT
Raw View
"Francis Glassborow" <francis.glassborow@ntlworld.com> wrote in message
news:UHjg9jAQF0m7EwHc@ntlworld.com...
> >The class base_c is an abstract class...therefore any classes derived
from
> >base_c must implement the pure virtual functions func() and func(char) or
> >they too will become abstract classes.
>
> Not really. Any class which has a direct (non-virtual) base that is
> abstract must implement all the pure virtual functions declared in that
> base or, in turn, be an abstract base class.

I don't see the difference between your explanation and mine...

> >
> >The problem seems to be in the compiler not giving you the proper error
> >message...by your definition of top_c you really should be implementing
both
> >versions of func(), but you're not. **Therefore top_c should become an
> >abstract class, and you shouldn't be able to create an object of type
> >top_c.**
>
> Sorry, but that is simply untrue. top_c inherits from a concrete class
> and has no pure virtual functions of its own and so is a concrete class.

[snip]

> The problem had nothing to do with the functions being virtual. Any time
> you declare an overrider for an inherited member function that is part
> of an overload set, that declaration hides all other members of the
> overload set unless you take explicit action such as a using
> declaration.

You are correct...and I am wrong...I realized this about 1/2 hour after
writing the post. I was a little hasty in my advice due to the volume of
e-mail I had to answer.

Please, folks, do not attempt to give advice that requires any amount of
thought if you don't have the proper time in which to do it.

:B

Peter
<^_^>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Dean Wakerley" <dean@housefloors.co.uk>
Date: Tue, 4 Sep 2001 17:50:57 GMT
Raw View
Apologies for the omission in previous posting, it kinda got left out when I
"Noddy"fied the code out of a larger project.

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: public base_c
{
public:
  void func() { cout << "middle_c::func()" << endl; };
  void func(char) { cout << "middle_c::func(char)" << endl; };
};

class top_c: public middle_c
{
public:
  void func() { cout << "top_c::func()" << endl; };
  /* void func(char);
   * use function inherited from middle_c
   */
};

int
main(/*...*/)
{
  top_c t;
  t.func();    // works
  t.func('x'); // problem: no matching function???
  return 0;
}

// ----- end of code ---------

Is this a language problem or a compiler problem?, and how do I get around
it?

TIA,
Dean


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]