Newbie inheritance question

Discussion in 'C++' started by keith@bytebrothers.co.uk, Jul 20, 2007.

  1. Guest

    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?
    , Jul 20, 2007
    #1
    1. Advertising

  2. Guest

    On 20 Jul, 09:04, 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...
    , Jul 20, 2007
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Jul 20, 2007
    #3
  4. Guest

    On 20 Jul, 10:16, Kai-Uwe Bux <> wrote:
    > 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.
    , Jul 20, 2007
    #4
  5. Kai-Uwe Bux Guest

    wrote:

    > On 20 Jul, 10:16, Kai-Uwe Bux <> wrote:
    >> 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
    Kai-Uwe Bux, Jul 20, 2007
    #5
  6. Guest

    On 20 Jul, 11:54, Kai-Uwe Bux <> 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 :)
    , Jul 20, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,134
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    814
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,035
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,892
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    768
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page