Call of overloaded function is ambiguous

This is a discussion on Call of overloaded function is ambiguous within the C++ Programming forums, part of the General Programming Boards category; So that is the error that I am getting. I know what it means and I slightly have an idea ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    48

    Call of overloaded function is ambiguous

    So that is the error that I am getting. I know what it means and I slightly have an idea of why I am getting it, but I do not know how to correct it.

    Let me introduce the players in this scene.

    Code:
    class Instruction
    {
    ...
    };
    typedef boost::shared_ptr<Instruction> InstructionPtr;
    
    class ReservationStation
    {
         ...
      public:
         InstructionPtr instr;
         ...
    };
    typedef boost::shared_ptr<ReservationStation> RSPtr;
    
    class ReservationStationList : private std::list<RSPtr>
    {
       public:
          void push_back(InstructionPtr instr);
          void push_back(RSPtr rs);
          .....
      private:
          using std::list<RSPtr>::push_back;
          ...
    }
    
    class OOOQueue
    {
      public:
        ...
        void update()
        {
             for (ReservationStationList::iterator p = list.begin(); p != list.end(); ++p)
             {
              if ((*p)->ready())
                 ready_list.push_back(*p);
               }
                ready_list.sort();
          }
      
        private:
            ReservationStationList list;
            ReservationStationList ready_list;
    }
    So I have an Instruction class and shared pointers to them called InstructionPtr. Then I have a ReservationStation class that contains an InstructionPtr through composition and shared pointers to ReservationStations called RSPtr. I then have a RSPtr list class and finally an OOOQueue class that contains two RSPtr lists. I am getting this error at the line
    Code:
    ready_list.push_back(*p);
    I am intending to call the publicly defined
    Code:
    void push_back(RSPtr rs);
    version.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So, a simpler example would be:

    Code:
    #include <list>
    
    class IntList: private std::list<int>
    {
       public:
            void push_back(int);
       private:
            using std::list<int>::push_back;
    };
    
    int main()
    {
        IntList li;
        li.push_back(10);
    }
    I suppose the problem is that li.push_back(10) matches both push_back methods, even though one is private.

    Why would you use the using statement in the first place? How are you going to use push_back in your class, where both are accessible?

    -------

    In any case, you have two overloads because the signatures are not identical (STL passes things by const reference), but that difference is not enough to disambiguate the call.

    If you make push_back take a const reference, it appears it would completely hide the parent's push_back method, so the using statement doesn't help anyway.

    Code:
    #include <iostream>
    #include <list>
    
    class IntList: private std::list<int>
    {
       public:
            void push_back(const int& i) {
                std::cout << "Pushing " << i << '\n';
                std::list<int>::push_back(i); //still got to spell it out fully, or it'll go into recursion
            }
       private:
            using std::list<int>::push_back; //useless
    };
    
    int main()
    {
        IntList li;
        li.push_back(10);
    }
    Last edited by anon; 11-21-2009 at 08:53 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Ok, so maybe I had the wrong idea about why I was getting the error. I figured it had to be due to the relationship between RSPtr and InstructionPtr because the third function was private.

    Here is my implementation for push_back(RSPtr)
    Code:
    void ReservationStationList::push_back(RSPtr rs)
    {
        if (size() != max_sz)
           std::list<RSPtr>::push_back(rs);
        else
            std::cout << "Queue is Full!" << std::endl;
    }
    So if I change the name of my public functions then I should be good? Are there any other solutions?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Looking at the code I just posted, I noticed something. I'm not sure if this is right, but if I use std::list<RSPtr>:ush_back then I am calling the scope of std::list and not ReservationStationList. The effect of that is that a new list will be created, not the calling object. Is that correct?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It should only mean that you are using std::list's push_back method on the current object, not the overridden push_back method you are defining.

    Code:
    this->std::list<int>::push_back(i);
    Note that that implementation is rather poor. Simply displaying messages is not a proper way to handle errors - yes, the user sees some error messages, but your program and the caller of push_back cannot respond properly.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    I renamed the public push_backs and then just used std::lists's push_back in my implementation of them and everything seems to be ok.

    How would I properly implement that if I wanted to keep all three functions with the same name?

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Proper implementation might look like this:

    Code:
    void ReservationStationList::push_back(const RSPtr& rs)
    {
        if (size() != max_sz)
           std::list<RSPtr>::push_back(rs);
        else
            throw std::runtime_error("Queue is Full!");
    }
    If the condition is not met, an exception is thrown (or you could return true/false). You have to let the rest of the program know that something went wrong.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 06:51 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. ambiguous call to overloaded function
    By LurPak in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2003, 03:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21