Ambiguous call to overloaded function

Discussion in 'C++' started by Joseph Turian, Dec 20, 2007.

  1. I have a templated class with the following methods:
    Vocab(const T& t);
    Vocab(unsigned uid);

    However, when T = unsigned, and I call Vocab(unsigned(0)) then the
    compiler rightly complains about an ambiguous call to the overloaded
    function.
    How can I specify which method I want called in this case?

    Thanks,
    Joseph
     
    Joseph Turian, Dec 20, 2007
    #1
    1. Advertising

  2. Joseph Turian

    Salt_Peter Guest

    On Dec 19, 11:45 pm, Joseph Turian <> wrote:
    > I have a templated class with the following methods:
    > Vocab(const T& t);
    > Vocab(unsigned uid);
    >
    > However, when T = unsigned, and I call Vocab(unsigned(0)) then the
    > compiler rightly complains about an ambiguous call to the overloaded
    > function.
    > How can I specify which method I want called in this case?
    >
    > Thanks,
    > Joseph



    One way is by using a dummy variable to change the member function's
    signature
    ie:
    template < typename T >
    class Test
    {
    ...
    public:
    void Vocab(const T& t);
    void Vocab(unsigned u, int dummy);
    };

    The better way might be to reduce the confusion:

    template < typename T >
    class Test
    {
    ...
    public:
    void setT(const T& t);
    void setuid(unsigned u);
    };
     
    Salt_Peter, Dec 20, 2007
    #2
    1. Advertising

  3. > The better way might be to reduce the confusion:
    > [...]


    I believe that he wants to do it in a constructor.

    > However, when T = unsigned, and I call Vocab(unsigned(0)) then the
    > compiler rightly complains about an ambiguous call to the overloaded
    > function.
    > How can I specify which method I want called in this case?


    Hmm, given that one is const and the other is not, does the explicit
    keyword work?
     
    Ioannis Gyftos, Dec 20, 2007
    #3
  4. Joseph Turian

    Ian Collins Guest

    Joseph Turian wrote:
    > I have a templated class with the following methods:
    > Vocab(const T& t);
    > Vocab(unsigned uid);
    >
    > However, when T = unsigned, and I call Vocab(unsigned(0)) then the
    > compiler rightly complains about an ambiguous call to the overloaded
    > function.
    > How can I specify which method I want called in this case?
    >

    Call it indirectly through a function template.

    The most specialised function matches first, so a regular function
    matches before a function template.

    --
    Ian Collins.
     
    Ian Collins, Dec 20, 2007
    #4
  5. On Dec 20, 8:07 am, Ian Collins <> wrote:
    > Joseph Turian wrote:
    > > I have a templated class with the following methods:
    > > Vocab(const T& t);
    > > Vocab(unsigned uid);

    >
    > > However, when T = unsigned, and I call Vocab(unsigned(0)) then the
    > > compiler rightly complains about an ambiguous call to the overloaded
    > > function.
    > > How can I specify which method I want called in this case?

    >
    > Call it indirectly through a function template.
    >
    > The most specialised function matches first, so a regular function
    > matches before a function template.
    >
    > --
    > Ian Collins.


    Clever, but pity the poor maintenance programmer !
    Giving the functions different names is less elegant but better for
    posterity ...
     
    tragomaskhalos, Dec 20, 2007
    #5
    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. ishekara

    ambiguous call to overloaded

    ishekara, Feb 25, 2005, in forum: C++
    Replies:
    7
    Views:
    6,517
    red floyd
    Feb 25, 2005
  2. rolandz
    Replies:
    5
    Views:
    522
    Jim Langston
    Jul 4, 2006
  3. , India
    Replies:
    1
    Views:
    409
    Kai-Uwe Bux
    Sep 10, 2007
  4. vinay.muthyal
    Replies:
    0
    Views:
    469
    vinay.muthyal
    Feb 26, 2008
  5. Ruki
    Replies:
    1
    Views:
    701
Loading...

Share This Page