简体   繁体   中英

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

I am trying to create some conversion functions for two classes by overloading the '=' operator. Here is some code:

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

How can I make this work?

Use forward declaration:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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