String.h has the following operator overloading function which is an user defined //declaration
int operator==(char *ch) //definition
int String::operator==(char *ch)
{
if (strcmp(_text, ch) == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
The above operator overloading function is called in the file Sample.c The g++ compiler throws the following error. String.h:67: note: candidate 1: int String::operator==(char*)
Sample.c:230: note: candidate 2: operator==(const char*, const char*) <built-in>
Sample.c:249: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
i have pasted the caller code (sample.c)
230 if (String(StrInputBuffer.part_string(4)) == ":86:")
{
//statement
}
249 else if (String(StrInputBuffer.part_string(1)) == ":")
{
}
I used the below gnu command to compile the source file
>g++ -c -g Sample.c -o samplecompiler version:
Target: x86_64-redhat-linux
gcc version 4.1.2 20080704 (Red Hat 4.1.2-54)
Could you please help me to resolve the issue?
You didn't post enough code to reproduce the problem, but I guess that somewhere in the class String there is a user-defined conversion function operatorchar*(). The easiest resolution is to remove it.
Thanks JLBorges,
Is it enough to modify the code as suggested by you //declaration
int operator==(const char *ch) const; //definition
int String::operator==(const char *ch)const
{
}
1. Do i need to add the below line in my code??
operator const char* () const ;
2.Is changes required in the below caller code??
if (String(StrInputBuffer.part_string(4)) == ":86:")
> Do i need to add the below line in my code?? operatorconstchar* () const ;
No, you do not need to add it.
(I had replied under the assumption that it was already there in your code.)
> Is changes required in the below caller code??
Hard to say for sure without seeing more of the code.
As it is, I would say: if this code if (String(StrInputBuffer.part_string(4)) == ":86:") is working correctly, as you expect it to work, let it be.
I think that the problem is that you have no overload operator
int String::operator==(const char *ch) const;
When you use this statement
if (String(StrInputBuffer.part_string(4)) == ":86:")
the right operand of the comparision operator has type const char * . And it seems that class String has a conversion function that converts an object of type String to type const char *. So the compiler does not know which operator-function to select. Either to convert the second operand ":86:" to type char * and use the user defined operator == or to convert the first operand to const char * and use built-in comparision operator for pointers.
I think you should define your comparision operator as
int String::operator==(const char *ch) const;
In fact it is general problem when a class has conversion functions. They are usually the reason of an ambiguity.