operator[] ambiguous

Michael Gong gongweigang@gmail.com
Fri Sep 24 23:17:00 GMT 2010


I have following program:

class A

  A(const char * a);

  operator const char*() const {return data_;}                //the
error message will go away if removing this line

  char& operator[](unsigned int);

  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 ?



More information about the Gcc-help mailing list