473,378 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

error C2666 overloads have similar conversions

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
5 11574
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BCC | last post by:
Hi, I have some very simple code: class CTest { public: CTest(void) {}; ~CTest(void) {}; };
1
by: BCC | last post by:
Hi, I have a large mfc project that has been working fine. A while ago, I added an enum and I want to make a vector of these enums: class MyClass { public: MyClass() {}; ~MyClass() {}; ...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
25
by: Chad Z. Hower aka Kudzu | last post by:
I have defined some implicit convertors so that I can do: MyStruct x = 4; The implicit convert the 4 into MyStruct. But its essentially a copy constructor and thus if I had: MyStruct x;...
5
by: Gang Zhang | last post by:
Hi, I have 2 overloaded functions as below: myfunc(byval val as Decimal) as string myfunc(byval val as String) as string When calling the function with a single like: dim num1 as single...
4
by: Gary Paris | last post by:
Why would you use an Overloads routine instead of just putting the code in the default routine? Private Overloads Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As...
3
by: John Shell | last post by:
Hello, all. The following code results in a C2666 error (2 overloads have similar conversions). class FSVec2D { public: FSVec2D() { // code omitted }
2
by: adsci | last post by:
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
15
by: Jason Doucette | last post by:
If you have two overloaded functions: double maximum(double a, double b, double c); int maximum(int a, int b, int c); They work fine if you call maximum() with all arguments as doubles, or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.