Thread: Ambiguous function call

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Ambiguous function call

    If someone wouldn't mind here...
    I'm getting an ambiguous function call here due to two overloaded operators and would like to know if there's a way around it.

    The ambiguous line is:
    Code:
    hFileDirs = FindFirstFile(pArgs->strFolder + _T("\\*.*"), &fDataDirs);
    pArgs->strFolder is Strings::CStringEx

    And the overloads in Strings::CStringEx are:
    Code:
    CTmplString operator + (const T* strData) const;
    CTmplString operator + (const CTmplString& rSrc);
    CStringEx is just a typedef for:
    Code:
    typedef CTmplString<TCHAR> CStringEx;
    And the compile errors I get:
    Code:
    error C2666: 'Strings::CTmplString<T>::operator +' : 2 overloads have similar conversions
    1>        with
    1>        [
    1>            T=wchar_t
    1>        ]
    1>        could be 'Strings::CTmplString<T> Strings::CTmplString<T>::operator +(const Strings::CTmplString<T> &)'
    1>        with
    1>        [
    1>            T=wchar_t
    1>        ]
    1>        or       'Strings::CTmplString<T> Strings::CTmplString<T>::operator +(const T *) const'
    1>        with
    1>        [
    1>            T=wchar_t
    1>        ]
    1>        while trying to match the argument list '(Strings::CStringEx, const wchar_t [5])'
    1>        note: qualification adjustment (const/volatile) may be causing the ambiguity
    Any idea what to do about this situation?
    I'm looking for any ideas/advice.
    Thanks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just a wild guess based on the "qualification adjustment (const/volatile) may be causing the ambiguity" note in the error message: what if you make this const correct:
    Code:
    CTmplString operator + (const CTmplString& rSrc);
    by changing it to:
    Code:
    CTmplString operator + (const CTmplString& rSrc) const;
    ?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Right! I forgot about that. I always tend to forget putting const in some places.
    Yep, now it compiles fine. Thanks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why is binary + a member at all? If you have a proper +=, + should always be implemented in terms of that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm not 100&#37; sure what you mean. But I did overload operator + to allow the above code. Internally in the class, it will just construct a temp object and call operator +=.
    But I also realized I don't need a operator + for the object itself. A constant character pointer is enough.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm not 100&#37; sure what you mean. But I did overload operator + to allow the above code. Internally in the class, it will just construct a temp object and call operator +=.
    This is, uh, related to the idea of preferring to write functions as non-member non-friend functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, but also, syntax such as 'a' == mystr is not to my liking, so I prefer to have the operators as member functions, because I can get away with it.
    My test suite hasn't let me down yet.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, but also, syntax such as 'a' == mystr is not to my liking
    You could enforce that by declaring as explicit the constructor that takes a char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm not trying to hinder functionality :O
    I'm merely avoiding implementing functionality that I don't need.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Elysia View Post
    I'm not 100% sure what you mean. But I did overload operator + to allow the above code. Internally in the class, it will just construct a temp object and call operator +=.
    But I also realized I don't need a operator + for the object itself. A constant character pointer is enough.
    Given
    Code:
    class foo
    {
    public:
      foo &operator +=(const foo &rhs);
    };
    The canonical way of implementing + is like this:
    Code:
    foo operator +(const foo &lhs, const foo &rhs)
    {
      foo t(lhs);
      t += rhs;
      return t;
    }
    Or, if your most important compiler doesn't support NRVO:
    Code:
    foo operator +(foo lhs, const foo &rhs)
    {
      return lhs += rhs;
    }
    The pattern is so ubiquitous that Boost even has it pre-packaged in the Operators library.
    Code:
    class foo : public boost::addable<foo>
    {
    public:
      foo &operator +=(const foo &rhs);
    };
    No operator+ at all. It's implemented by the library.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The right-hand is just added to the left-hand in other words.
    So in order to skip operator +, I would just derive from boost::addable?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yep. And by deriving from boost::addable<ExString, const char *> you even get the mixed versions for free.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Cool! Will have to try that out when I get home.
    So much good stuff hidden in boost that I really don't know!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM