error C2666 overloads have similar conversions

W

wongjoekmeu

Hello All,
I am trying to poort a code which is written in Visual C++ 6.0 to
Visual C++ in VIsual Studio .NET
I receive a problem which I simply do not understand. I was wondering
if anyone could help me out and explain me the problem I am receiving.

I have a derive class called Mystring derived from the base std::string
class.

it looks like this
---------------
class Mystring : public std::string
{
public:
----
----
bool operator ==( const std::string& rhs ) const;
bool operator ==( const Mystring& rhs ) const;
---
---
};
---------
Now when I use this operator as follow:

if( ObjectOfMystring == "Same")

---------
I receive no error when I use to compile with Visual C++ 6.0 but when I
compile with Visual C++ from Visual Studion .NET I receive the
following error:

error C2666 'Mystring::eek:perator'==" : 3 overloads have similar
conversions

Can anyone please explain to me what this problem is about. Many thanks
in advance.

Robert
 
R

Real Name

Hi,

[snip]
error C2666 'Mystring::eek:perator'==" : 3 overloads have similar
conversions

could be:
1.) std::string::eek:perator==(const char*) const
2.) Mystring::eek:perator==(const std::string&) const (with implicit conversion
from const char* to std::string)
3.) Mystring::eek:perator==(const Mystring&) const (with implicit conversion
from const char* to Mystring)

Can anyone please explain to me what this problem is about. Many thanks
in advance.

Try it with another function with signature
bool Mystring::eek:perator==(const char* rhs) const { return
std::string::eek:perator==(rhs);}


TY
 
W

wongjoekmeu

I still do not understand what is wrong with the code. What I speculate
though, is that for some reason it is not happy with the conversion of
the "SAME" character string to a string& or Mystring&. Or could it be
that it does not know which overload function to use ? I suppose both
classes ( since Mystring is derive from string class) know how to
implicitly convert "SAME" to a string type. But I am still not sure
whether I am correct with my speculations. Can anyone explain it in
detail to me this error ? And how to solve it properly.

Robert.
 
A

Attila Feher

I still do not understand what is wrong with the code. What I
speculate though, is that for some reason it is not happy with the
conversion of the "SAME" character string to a string& or Mystring&.
Or could it be that it does not know which overload function to use ?
I suppose both classes ( since Mystring is derive from string class)
know how to implicitly convert "SAME" to a string type. But I am
still not sure whether I am correct with my speculations. Can anyone
explain it in detail to me this error ? And how to solve it properly.

As far as I understand there is nothing wrong with the code. The problem is
that the 3 overloads are equally good. :) If one of them would be better
than the others, you won't have ambiguity. But in that code right now all 3
functions need an implicit conversion (as I understand it) and the compiler
refuses to guess which is the one what you've meant by that expression.
 
A

Andrey Tarasevich

Hello All,
I am trying to poort a code which is written in Visual C++ 6.0 to
Visual C++ in VIsual Studio .NET
I receive a problem which I simply do not understand. I was wondering
if anyone could help me out and explain me the problem I am receiving.

I have a derive class called Mystring derived from the base std::string
class.

it looks like this
---------------
class Mystring : public std::string
{
public:
----
----
bool operator ==( const std::string& rhs ) const;
bool operator ==( const Mystring& rhs ) const;
---
---
};
---------
Now when I use this operator as follow:

if( ObjectOfMystring == "Same")

---------
I receive no error when I use to compile with Visual C++ 6.0 but when I
compile with Visual C++ from Visual Studion .NET I receive the
following error:

error C2666 'Mystring::eek:perator'==" : 3 overloads have similar
conversions

Can anyone please explain to me what this problem is about. Many thanks
in advance.
...

You didn't provide enough information. Is your 'MyString' class
implicitly convertible from 'const char*' value? In other words, do you
have a non-'explicit' conversion constructor of the form

Mystring::Mystring(const char* lpsz);

or equivalent in your 'Mystring' class?
 
W

wongjoekmeu

Hello Andrey,
I do have the following constructors, taking either a "const char" or
"const char*". Here below you can see how it is declared.
--------------------------------------
Mystring::Mystring(const char arg)
{
this->assign( 1, arg );
}

Mystring::Mystring(const char *arg)
{
this->assign( arg );
}
---------------------------------------
By the way I have to say that I have done the following thing to not
let the compiler complaints.
I did the following. Instead of saying

if(ObjectOfMystring == "Same")

if first declared an object string and assign it to "Same"

string temp_Same = "Same"
if(ObjectOfMystring == temp_Same)

And now the compiler does not complaint. It seems like now, it knows it
is of type string. I assume that this is correct, if both conversation
types were indeed evenly ok either string or Mystring. I suppose that
by explicitly telling it is of type string won't change my program
purpose. Right ??
Thank you for answering my question. I hope that now I have supplied
enough information and will receive the from someone the correct answer
why the compiler was complaining.

Robert
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top