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

标签 c++ declaration operator-keyword

我有代码:

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/

相关文章:

c++ - AttributeError :/usr/lib/libgdal. so.1: undefined symbol :OGR_F_GetFieldAsInteger64 在 Docker 中安装时

c++ - 引用成员长序列的效率

c++ - 前面的行在 ofstream 上被清除

c++ - 如何将 C++ 目标文件与 ld 链接

c - 动态矩阵作为函数的静态参数

c++ - 对最终用户无法访问的模板化和非模板化功能的混合进行适当的组织

C++ 命名空间、内部类和运算符解析

Python += 与全局变量函数内的 .extend()

java - Angular 开始(ReferenceError : angular is not defined)

swift - 如何定义自定义下标数组运算符,必​​要时使数组元素为 "spring into existence"