3 overloads have similar conversions Error

Discussion in 'C++' started by adsci, Feb 16, 2007.

  1. adsci

    adsci Guest

    Hello!

    Im posting this to c.l.c++ AND win32 because i dont know if this is a MS
    Compiler Issue or not.

    here we go:

    <code>

    class MyCString
    {

    MyCString ();
    ~MyCString ();
    MyCString (const CString &param);
    MyCString (LPCTSTR param);
    MyCString &operator=(const CString &param);
    MyCString &operator=(LPCTSTR param);
    operator LPCTSTR () const;
    .
    .
    .
    };

    bool operator==(const CString &param1, const CString &param2);
    bool operator==(const CString &param1, LPCTSTR param2);

    ....

    TCHAR filefolder[MAXFOLDERLEN];
    MyCString foldername;

    if(filefolder == foldername) // error C2666: '==' : 3 overloads have
    similar conversions

    ..
    ..
    ..

    </code>

    LPCTSTR = const short * with current switches.

    i do not how to tell the compiler that *no* conversion should be used
    with the global == operators. if i delete the custom conversion operator
    everything runs fine.

    when i kill both == operators it compiles nice, but i guess on compare
    it will compare the pointers and not the strings, right?

    any suggestions?

    THANK YOU!

    Marcel
     
    adsci, Feb 16, 2007
    #1
    1. Advertising

  2. adsci

    peter koch Guest

    On Feb 16, 11:52 am, adsci <> wrote:
    > Hello!
    >
    > Im posting this to c.l.c++ AND win32 because i dont know if this is a MS
    > Compiler Issue or not.
    >
    > here we go:
    >
    > <code>
    >
    > class MyCString
    > {
    >
    > MyCString ();
    > ~MyCString ();
    > MyCString (const CString &param);
    > MyCString (LPCTSTR param);
    > MyCString &operator=(const CString &param);
    > MyCString &operator=(LPCTSTR param);
    > operator LPCTSTR () const;

    That one is a bad idea. There is a reason why the standard chose the
    explicit conversion with c_str().
    > .
    > .
    > .
    >
    > };
    >
    > bool operator==(const CString &param1, const CString &param2);
    > bool operator==(const CString &param1, LPCTSTR param2);

    Why no
    bool operator==(LPCTSTR param1, const CString &param2)?
    >
    > ...
    >
    > TCHAR filefolder[MAXFOLDERLEN];
    > MyCString foldername;
    >
    > if(filefolder == foldername) // error C2666: '==' : 3 overloads have
    > similar conversions

    Well - it looks for a operator==(LPCTSTR,MyCString), and it finds
    bool operator==(const CString &param1, const CString &param2);
    (using your constructor for the first parameter) and
    bool operator==(LPCTSTR param1, LPCTSTR param2);
    ("built-in" as LPCTSTR is a native type).
    I do not see the third overload since your other userdefined
    operator== requires two conversions, but that is probably a blind spot
    on my side (or a misinterpretation of the diagnostic).

    > i do not how to tell the compiler that *no* conversion should be used
    > with the global == operators. if i delete the custom conversion operator
    > everything runs fine.


    You can not in the general case prevent conversions unless you write
    operators that cater for all cases.
    >
    > when i kill both == operators it compiles nice, but i guess on compare
    > it will compare the pointers and not the strings, right?

    Yes.
    >
    > any suggestions?

    Add the third operator== and remove the implicit conversion to
    LPCTSTR. That one is going to bite you.

    /Peter
     
    peter koch, Feb 16, 2007
    #2
    1. Advertising

  3. adsci

    adsci Guest

    peter koch schrieb:
    > On Feb 16, 11:52 am, adsci <> wrote:
    >> Hello!
    >>
    >> Im posting this to c.l.c++ AND win32 because i dont know if this is a MS
    >> Compiler Issue or not.
    >>
    >> here we go:
    >>
    >> <code>
    >>
    >> class MyCString
    >> {
    >>
    >> MyCString ();
    >> ~MyCString ();
    >> MyCString (const CString &param);
    >> MyCString (LPCTSTR param);
    >> MyCString &operator=(const CString &param);
    >> MyCString &operator=(LPCTSTR param);
    >> operator LPCTSTR () const;

    > That one is a bad idea. There is a reason why the standard chose the
    > explicit conversion with c_str().


    yes, seems so. its just that MS' implementation of CString does it.
    quote from AFX.H:

    <quote>
    // return pointer to const string
    operator LPCTSTR() const;
    </quote>

    i needed a CString replacement to port some code. i created a
    c_str()-like function too, but was curious whether i can do it with an
    conversion also.

    finally i got it working. everything works fine, when i add
    the bool operator==(LPCTSTR param1, const CString &param2)
    operator. thank you for pointing that out.

    >> .
    >> .
    >> .
    >>
    >> };
    >>
    >> bool operator==(const CString &param1, const CString &param2);
    >> bool operator==(const CString &param1, LPCTSTR param2);

    > Why no
    > bool operator==(LPCTSTR param1, const CString &param2)?


    yeah, i was under time pressure and decided to implement the "needed"
    parts of CString only. was a fault. it wasnt clear to me that the error
    could emerge from the missing operator overload.

    >> ...
    >>
    >> TCHAR filefolder[MAXFOLDERLEN];
    >> MyCString foldername;
    >>
    >> if(filefolder == foldername) // error C2666: '==' : 3 overloads have
    >> similar conversions

    > Well - it looks for a operator==(LPCTSTR,MyCString), and it finds
    > bool operator==(const CString &param1, const CString &param2);
    > (using your constructor for the first parameter) and
    > bool operator==(LPCTSTR param1, LPCTSTR param2);
    > ("built-in" as LPCTSTR is a native type).
    > I do not see the third overload since your other userdefined
    > operator== requires two conversions, but that is probably a blind spot
    > on my side (or a misinterpretation of the diagnostic).


    yes.

    >
    >> i do not how to tell the compiler that *no* conversion should be used
    >> with the global == operators. if i delete the custom conversion operator
    >> everything runs fine.

    >
    > You can not in the general case prevent conversions unless you write
    > operators that cater for all cases.


    thats the point. thank you.

    >> when i kill both == operators it compiles nice, but i guess on compare
    >> it will compare the pointers and not the strings, right?

    > Yes.
    >> any suggestions?

    > Add the third operator== and remove the implicit conversion to
    > LPCTSTR. That one is going to bite you.


    seems like working okay WITH the conversion as long as i have
    operator=='s for all circumstances.

    again: thank you!

    Regards
    Marcel
     
    adsci, Feb 16, 2007
    #3
    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. Fabio Negri Cicotti [MCP]

    Overloads modifier

    Fabio Negri Cicotti [MCP], Dec 13, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    491
    William F. Robertson, Jr.
    Dec 13, 2004
  2. Replies:
    5
    Views:
    10,004
  3. Bengt Richter
    Replies:
    3
    Views:
    293
    Steve Holden
    Jan 19, 2005
  4. Replies:
    10
    Views:
    714
    Jasen Betts
    Aug 5, 2005
  5. Angus
    Replies:
    2
    Views:
    1,507
    Ron Natalie
    Jan 7, 2007
Loading...

Share This Page