3 overloads have similar conversions Error

A

adsci

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
 
P

peter koch

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
 
A

adsci

peter said:
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.
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top