c++ - 在 C++ 中声明类之前如何使用它?

标签 c++ class operator-overloading

我正在尝试通过重载“=”运算符为两个类创建一些转换函数。这是一些代码:

class Vertex {
public:
    int X, Y;
        // .......
    Vertex& operator= (const VertexF &);   // ERROR, VertexF is not declared
};

class VertexF {
public:
    float X, Y;
        // ......
    VertexF& operator= (const Vertex &);
};

我怎样才能让它工作?

最佳答案

使用前向声明:

class VertexF; // forward declaration of VertexF

class Vertex {
public:
    int X, Y;
        // .......
    Vertex& operator= (const VertexF &);   // ERROR, VertexF is not declared
};

class VertexF {
public:
    float X, Y;
        // ......
    VertexF& operator= (const Vertex &);
};

关于c++ - 在 C++ 中声明类之前如何使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9238906/

相关文章:

c++ - 如何在 C++ 中初始化 char* const argv []

c++ - 为什么子类调用它父类的方法而不是它自己的?

c++ - 调用对象的析构函数是否等同于在对象上调用delete?

c# - 如何使用 C# 在类文件中写入连接字符串?

python - 动态重载运算符

c++ - 推导 lambda 函数中的模板参数及其结果

c++ - 转发引用的参数类型检查不起作用

c++ - C/C++ ncurses 卡住

python - += python 中 __setitem__ 的运算符(添加到方括号中)

c++ - 如何在不违反C++中的DRY原则的情况下实现可移动重载?