Yes. I changed the [] function to "int", and it worked. Thanks.
--------------------------------------------------
From: "Andrew Bell" <andrew.bell.ia@xxxxxxxxx>
Sent: Friday, September 24, 2010 3:37 PM
To: "Michael Gong" <gongweigang@xxxxxxxxx>
Cc: <gcc-help@xxxxxxxxxxx>
Subject: Re: operator[] ambiguous
Michael,
The compiler can't decide whether to cast abc to a const char * and
then apply [] or use the [] function you provided. To fix, either
cast 0 to unsigned, or change the argument of your [] function to take
an int instead of an unsigned.
I'm not sure how this relates to the standard.
On Fri, Sep 24, 2010 at 2:01 PM, Michael Gong <gongweigang@xxxxxxxxx>
wrote:
Hi,
I have following program:
class A
{
public:
A(const char * a);
operator const char*() const {return data_;} //the
error message will go away if removing this line
char& operator[](unsigned int);
protected:
private:
char* data_;
};
int main() {
A abc("kkk");
abc[0] = 'x';
//this is the line causing the error message
return 0;
}
If I compile with g++, it has following error message:
ISO C++ says that `char& A::operator[](unsigned int)' and
`operator[]' are ambiguous even though the worst conversion for the
former is better than the worst conversion for the latter
But when I compile it using SunStudio compiler, it works.
I am not quite familar with ISO C++. Could anyone explain where the
ambiguous come from ?
Thanks.
Michael
--
Andrew Bell
andrew.bell.ia@xxxxxxxxx