【发布时间】:2012-07-02 12:45:41
【问题描述】:
我有代码:
class Point3D{
protected:
float x;
float y;
float z;
public:
Point3D(){x=0; y=0; z=0;}
Point3D(const Point3D & point){x = point.x; y = point.y; z = point.z;}
Point3D(float _x,float _y,float _z){x = _x; y = _y; z = _z;}
}
class Vector3D{
protected:
Point3D start;
Point3D end;
public:
...
Point3D getSizes(){
return Point3D(end-start);
}
}
我想为 Point3D 创建一个带有矢量的 operator+:
Point3D & operator+(const Vector3D &vector){
Point3D temp;
temp.x = x + vector.getSizes().x;
temp.y = y + vector.getSizes().y;
temp.z = z + vector.getSizes().z;
return temp;
}
但是当我将该操作放在 Point3D 类声明中时,我得到了错误,因为我没有在这里声明 Vector3D。而且我不能在 Point3D 之前移动 Vector3D 声明,因为它使用 Point3D。
【问题讨论】:
-
Andrew 的答案可能是最好的选择,但由于
operator+正在引用,您可以使用前向声明 www-subatech.in2p3.fr/~photons/subatech/soft/carnac/… ,然后在Point3D内声明operator+并在下面定义 @ 987654327@.
标签: c++ declaration operator-keyword