Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie inheritance question

Reply
Thread Tools

Newbie inheritance question

 
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      07-20-2007

I posted something similar to this earlier in the week, but never saw
it appear, so I'll try again. My apologies if this appears to be a
dupe where you are.

If I have something like the following:

class Base
{
public:
void f(string&);
};

class Derived : public Base
{
public:
void f(char*);
};

int main()
{
string myStr = "abc";
Derived obj;

obj.f(myStr);
}

I get a compile-time error like this:

no matching function for call to `Derived::f (string &)'
candidates are: void Derived::f(char *)

Why does it not find the correctly matching function from the Base
class?

 
Reply With Quote
 
 
 
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      07-20-2007
On 20 Jul, 09:04, (E-Mail Removed) wrote:
> I posted something similar to this earlier in the week, but never saw
> it appear, so I'll try again. My apologies if this appears to be a
> dupe where you are.


Of course, the instant I post the repeat, I find the original. Sorry
people, and thanks to those who answered. The more recent example is
simplified, and has actually gone through a compiler, BTW.

I shall check out the article mentioned by Tim Love...

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-20-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>
> I posted something similar to this earlier in the week, but never saw
> it appear, so I'll try again. My apologies if this appears to be a
> dupe where you are.
>
> If I have something like the following:
>
> class Base
> {
> public:
> void f(string&);
> };
>
> class Derived : public Base
> {
> public:


using Base::f;

> void f(char*);
> };
>
> int main()
> {
> string myStr = "abc";
> Derived obj;
>
> obj.f(myStr);
> }
>
> I get a compile-time error like this:
>
> no matching function for call to `Derived::f (string &)'
> candidates are: void Derived::f(char *)
>
> Why does it not find the correctly matching function from the Base
> class?


It's hidden.


Best

Kai-Uwe Bux

 
Reply With Quote
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      07-20-2007
On 20 Jul, 10:16, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>
> > no matching function for call to `Derived::f (string &)'
> > candidates are: void Derived::f(char *)

>
> > Why does it not find the correctly matching function from the Base
> > class?

>
> using Base::f;
> It's hidden.


OK, now I just tried that with this code:

#include <string>
class Base
{
public:
void f(string&);
};

class Derived : public Base
{
public:
using Base::f;
void f(char*);
};

int main()
{
string myStr = "abc";
Derived obj;

obj.f(myStr);
}

This now gives me the compilation errors:

cannot adjust access to `void Base::f(string &)' in `class Derived'
because of local method `void Derived::f(char *)' with same name

In function `int main()':
no matching function for call to `Derived::f (string &)'
candidates are: void Derived::f(char *)

So, the 'namespace hack' doesn't work, and I still have the original
error

Any clues, please? Oh, and all the above relates to the venerable g++
v2.95.3, BTW.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-20-2007
(E-Mail Removed) wrote:

> On 20 Jul, 10:16, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>
>> > no matching function for call to `Derived::f (string &)'
>> > candidates are: void Derived::f(char *)

>>
>> > Why does it not find the correctly matching function from the Base
>> > class?

>>
>> using Base::f;
>> It's hidden.

>
> OK, now I just tried that with this code:
>
> #include <string>


using std::string;

> class Base
> {
> public:
> void f(string&);
> };
>
> class Derived : public Base
> {
> public:
> using Base::f;
> void f(char*);
> };
>
> int main()
> {
> string myStr = "abc";
> Derived obj;
>
> obj.f(myStr);
> }
>
> This now gives me the compilation errors:
>
> cannot adjust access to `void Base::f(string &)' in `class Derived'
> because of local method `void Derived::f(char *)' with same name


The above (with using std::string) is standard conforming and ought to
compile.
>
> In function `int main()':
> no matching function for call to `Derived::f (string &)'
> candidates are: void Derived::f(char *)
>
> So, the 'namespace hack' doesn't work, and I still have the original
> error
>
> Any clues, please? Oh, and all the above relates to the venerable g++
> v2.95.3, BTW.


Upgrade your compiler. The version current version of g++ is 4.x.y, which
compiles the snippet just fine. G++ has come a long way toward being more
standard conforming.


Best

Kai-Uwe Bux
 
Reply With Quote
 
keith@bytebrothers.co.uk
Guest
Posts: n/a
 
      07-20-2007
On 20 Jul, 11:54, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>
> Upgrade your compiler. The version current version of g++ is 4.x.y, which
> compiles the snippet just fine. G++ has come a long way toward being more
> standard conforming.


You are absolutely right. I found a copy of g++ v3.4.3 on one of our
boxes, and it compiles the code just fine. Unfortunately, I have to
use the old v2.95.3 for the current project. Oh well, it's the
client's problem

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments