Tibi
Tibi

Reputation: 3875

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?

Upvotes: 2

Views: 134

Answers (2)

Meysam
Meysam

Reputation: 18117

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

Upvotes: 9

Paul Nikonowicz
Paul Nikonowicz

Reputation: 3903

Separate the classes into separate files and have each reference the other in their respective header files.

Upvotes: 0

Related Questions