2012年5月16日 星期三

c++ error: operator []: overloads have similar conversions

template <class Type>
class Array
{
    Type&       operator[] ( unsigned int index );
}

Array<int> a1;
a1.pushback(10);
a1.pushback(20);

int g = a1[1];

c++ error: operator []:  3 overloads have similar conversions

Because the compiler can't determine "1" is int, const int, or unsigned int in the implicit conversion rule.

We need to cast it to unsigned int, let the compiler knows what we like to do.
sol:
     int g = a1[static_cast<unsigned int>(1)];


Reference:
http://stackoverflow.com/questions/1726740/c-error-operator-2-overloads-have-similar-conversions

沒有留言:

張貼留言