class CVector3{
private:
double e[3];
public:
friend CVector3 operator *(CVector3 &a, double p); //向量*常数
friend CVector3 operator +(CVector3 &a, CVector3 &b);
double &operator[](int);//元素操作
};
double &CVector3::operator[](int index){
if (index < 3 && index >= 0){
return e[index];
}
else{
static double err = 0.0;
return err;
}
}
和此网站:
http://nknucc.nknu.edu.tw/~jwu/c/cpgch14.htm
的例7相比,我多了 &operator[] 的操作
目的是想要较方便的存取 e[0~2]
但却造成我没办法做连续的向量运算,如:
CVector3 a1,a2,b(1,2,3),c(4,5,6);//建构时可写入初始值,上面程式码省略
a1=b+c; //这样OK
a1=b*2.0; //OK
a2=b+c*2.0; //错误
a2=b+(c*2.0); //也是错误
想请教这里错误的原因为何?