I have the following code:
Code:
struct TwoInts
{
int val[2];
int &operator [] (unsigned int);
TwoInts(int, int);
};
int &TwoInts::operator [] (unsigned int i) { return val[i]; }
TwoInts::TwoInts(int a, int b) { val[0] = a; val[1] = b; }
TwoInts operator + (const TwoInts &a, const TwoInts &b)
{
return TwoInts(a[0]+b[0], a[1]+b[1]);
}
int main(int argc, char *argv[]) { return 0; }
When I compile it, the only line of the + operator gives the following error four times:
14 C:\test\main.cpp passing `const TwoInts' as `this' argument of `int& TwoInts::operator[](unsigned int)' discards qualifiers
This despite the fact that it seems to me I'm doing nothing wrong. Sure, a and b are const, but I'm not modifying them, nor am I treating the float returned by the [] operator as non const. Any way to fix this?