CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Templates, multiple inheritance, and ambiguous overloads

    Let's say I have a templated base class:
    Code:
    template <typename Input>
    class Consumer
    {
    public:
        void consume(Input input)
        {
            // do something
        }
    };
    Now, I have a class which derives from this twice (A and B are unrelated, but otherwise unimportant classes):
    Code:
    class A;
    class B;
    
    class Derived: public Consumer<A>, public Consumer<B>
    {
    };
    Now, elsewhere in the code I do this:
    Code:
    {
        A myA;
        Derived myDerived;
        myDerived.consume(myA);
    }
    This looks like it should be simple enough, but g++ 4.8.1 is giving me a "request for member is ambiguous" error.

    Any idea why?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Templates, multiple inheritance, and ambiguous overloads

    I suspect I need to add these to Derived:
    Code:
    using Consumer<A>::consume;
    using Consumer<B>::consume;
    After reviewing what I'm actually trying to do, though, I'm going to make Consumer a variadic class instead of using multiple inheritance.

  3. #3
    Join Date
    Jul 2013
    Posts
    576

    Re: Templates, multiple inheritance, and ambiguous overloads

    Quote Originally Posted by Lindley View Post
    I suspect I need to add these to Derived:
    Code:
    using Consumer<A>::consume;
    using Consumer<B>::consume;

    Well I guess it means in this case overloading isn't enougth to separate methods and using is a way to overcome that.

    I didn't know that and I probably would've added this in Derived:

    Code:
    void consume(A input) {Consumer<A>::consume(input);}
    void consume(B input) {Consumer<B>::consume(input);}
    so thank you.
    Last edited by razzle; January 16th, 2014 at 07:35 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured