I'm testing overloading [] and + operator, but Expected unqualified-id occurs and I have no idea why.
BTW, I'm using google test on Xcode.
The error occurs on line 6 and 7
TEST (Speed, operatorPlus) {
Speed speed_0(3, 4);
Speed speed_1(5, 6);
Speed * speed_add = speed_0 + speed_1;
ASSERT_EQ(8, speed_add->[0]);
ASSERT_EQ(10, speed_add->[1]); }
And here's the Speed class:
class Speed {
public:
Speed (double x, double y) {
this->x = x;
this->y = y;
}
double getAbsSpeed () const {
return sqrt(x * x + y * y);
}
double & operator [] (int index) {
if (index == 0)
return x;
else if (index == 1)
return y;
else
throw (std::string)"index error";
}
Speed * operator + (Speed & s) const {
return new Speed(x + s[0], y + s[1]);
}
double & getX () {
return x;
}
double & getY () {
return y;
}
private:
double x = 0;
double y = 0;
};
The code works fine if I use getX(), but I'm not sure why can't I use ->[]