Newbie inheritance question

K

keith

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?
 
K

keith

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...
 
K

Kai-Uwe Bux

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
 
K

keith

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.
 
K

Kai-Uwe Bux

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
 
K

keith

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 :)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top