0

I'm new to c++ and i'm trying to make a simple game engine. I am trying to make a game object and component system.

I have a base class for all object and I am trying to store all children of the object in an array inside the class. But i get an error when trying to make an array of the class inside the class.

class Object {
    public: 
        Object children[]; //This is where I get the error
};

Is there a way to do this? If not, how can I work around this issue?

4
  • 1
    Whenever you think dynamic array when programming in C++, the next thought should be std::vector. Commented Jan 24, 2021 at 15:50
  • Also if you want any kind of polymorphism to work then you need to use pointers to the base class instead of instances of the base class. Commented Jan 24, 2021 at 15:50
  • 1
    In C++ arrays have to have a fixed size, your array has no size. Despite what you seem to think there is no dynamic array built in to the C++ language, The easy option is to use a vector, which is part of the standard library. Commented Jan 24, 2021 at 15:54
  • @john you cannot have a fixed sized array of one's own class. You would be attempting to construct an infinitely sized amount of memory. Commented Jan 24, 2021 at 18:55

1 Answer 1

1

Object children[] is a fixed size array. For these types of arrays, the size needs to be known at compile time or else an error will be flagged by the IDE (the compiler might ignore it as the size of it is 0 but would flag a warning).

You cannot do Object children[some_amount] as till be an incomplete type. So your option is to use a dynamically resizable array.

For this, C++ standard library provides an excellent option in the name of std::vector. To use this you must include the vector header file. The way to use it is simple, for your case you can use it by this: std::vector<Object> children; and elements can be added at runtime using the std::vector<>::push_back() function.

Further information: https://en.cppreference.com/w/cpp/container/vector

Sign up to request clarification or add additional context in comments.

6 Comments

curious why the downvote? is there any misleading information?
@thakeenathees Probably there was. I edited it with a better explanation :3
one thing Object children[100] is invalid inside the Object class since the class itself is incomplete at that point, could you edit that as well
Trying to store "all children of the object" (as described in the question) in the vector like this, will only end in tears. This is completely wrong. It's been marked as an accepted answer; so in the near future we can expect a follow-up question wondering about either mysterious compilation errors, or mysterious behavior of the resulting code.
@SamVarshavchik How would you recommend i store and structure the game object hierarchy?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.