By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

error C2666 overloads have similar conversions

P: n/a
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::operator'==" : 3 overloads have similar
conversions

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

Robert

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

[snip]
error C2666 'Mystring::operator'==" : 3 overloads have similar
conversions
could be:
1.) std::string::operator==(const char*) const
2.) Mystring::operator==(const std::string&) const (with implicit conversion
from const char* to std::string)
3.) Mystring::operator==(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::operator==(const char* rhs) const { return
std::string::operator==(rhs);}
TY
Jul 23 '05 #2

P: n/a
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.

Jul 23 '05 #3

P: n/a
wo*********@yahoo.com wrote:
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.

--
Attila aka WW
Jul 23 '05 #4

P: n/a
wo*********@yahoo.com wrote:
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::operator'==" : 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?

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #5

P: n/a
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

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.