Brans
Brans

Reputation: 649

Can't declare Class field type of the other class

Why i can't declare Class field type of the other class? This gives me C4430 error:

//Entity.h file
    class Entity
    {
    public:
        Box test;
    };


    class Box
    {
    public:
        double length;   // Length of a box
        double breadth;  // Breadth of a box
        double height;   // Height of a box
    };

Upvotes: 1

Views: 69

Answers (1)

Nemanja Boric
Nemanja Boric

Reputation: 22177

Class Entity needs to know about class Box, before the definition. Also, as you're including the object of and not the pointer to Box in your Entity class, it also needs to know the size of class Box (full definition of Box class is needed) and the definition of the members (as it will access Box::Box in order to initialize the actual field), so you need the full definition of Box before making it a field in your Entity class.

    class Box
    {
    public:
        double length;   // Length of a box
        double breadth;  // Breadth of a box
        double height;   // Height of a box
    };

    class Entity
    {
    public:
        Box test;
    };

Upvotes: 3

Related Questions