ISO C++ Ambiguous error

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 sample compiler 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?

Thanks for looking into this
Make the code const-correct and the error will go away.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string>

struct good
{
    bool operator== ( const char* ) const ;
    operator const char* () const ;
};

struct bad
{
    bool operator== ( char* ) ; // *** bad; not const-correct
    operator const char* () const ;
};

int main()
{
    good g ;
    g == "hello" ; // fine

    bad b ;
    b == "hello" ; // *** error ***
    /*  ambiguous overload for 'operator==' in 'b == "hello"'
        note: candidates are:
        note: operator==(const char*, const char*) <built-in>
        note: bool bad::operator==(char*) */
}
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 operator char*(). 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:")

Kindly advise me
> Do i need to add the below line in my code??
operator const char* () 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.





Last edited on
JLBorges (1721) :Thanks a lot for your help!!!!!!!!issue resolved
vlad from moscow (3630) :thank you for looking into this..issue resolved
Topic archived. No new replies allowed.