gpt4 book ai didi

c++ - Point 的运算符 +(Vector) - 但 Vector 使用 Point 并且在 Point 声明中未声明

转载 作者:太空狗 更新时间:2023-10-29 23:53:21 26 4
gpt4 key购买 nike

我有代码:

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+,它将采用一个 vector :

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。

最佳答案

把它放在类之外:

Point3D operator+(const Point3D &p, const Vector3D &v)
{

}

并且永远不会返回对局部变量的引用!

关于c++ - Point 的运算符 +(Vector) - 但 Vector 使用 Point 并且在 Point 声明中未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11293969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com