繁体   English   中英

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

[英]How can I use a class before it is declared in c++?

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

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 &);
};

将类分隔为单独的文件,并使每个文件在其各自的头文件中引用另一个文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM