Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 3 overloads have similar conversions Error

Reply
Thread Tools

3 overloads have similar conversions Error

 
 
adsci
Guest
Posts: n/a
 
      02-16-2007
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
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      02-16-2007
On Feb 16, 11:52 am, adsci <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
adsci
Guest
Posts: n/a
 
      02-16-2007


peter koch schrieb:
> On Feb 16, 11:52 am, adsci <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error C2665: 'delete' : none of the 2 overloads can convert parameter 1 from type 'const class xyz*' Angus C++ 2 01-07-2007 04:09 PM
Pointer conversions and Data types conversions vb@gmail.com C Programming 10 08-05-2005 09:51 AM
error C2666 overloads have similar conversions wongjoekmeu@yahoo.com C++ 5 02-04-2005 07:23 PM
Has apparent 2.4b1 bug been fixed? flatten in Lib\compiler\ast.py overloads 'list' name Bengt Richter Python 3 01-19-2005 05:17 PM
Overloads modifier Fabio Negri Cicotti [MCP] ASP .Net 3 12-13-2004 05:20 PM



Advertisments