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?

saile515
  • 21
  • 1
  • 1
  • 1
    Whenever you think *dynamic array* when programming in C++, the next thought should be [`std::vector`](https://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Jan 24 '21 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. – Some programmer dude Jan 24 '21 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. – john Jan 24 '21 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. – miguel.martin Jan 24 '21 at 18:55

1 Answers1

2

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

D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • curious why the downvote? is there any misleading information? – thakee nathees Jan 24 '21 at 16:04
  • @thakeenathees Probably there was. I edited it with a better explanation :3 – D-RAJ Jan 24 '21 at 16:05
  • 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 – thakee nathees Jan 24 '21 at 16:07
  • 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. – Sam Varshavchik Jan 24 '21 at 18:08
  • @SamVarshavchik How would you recommend i store and structure the game object hierarchy? – saile515 Jan 25 '21 at 20:21
  • See the following question for an explanation of the problem: https://stackoverflow.com/questions/48650599/c-stdvector-of-base-class-objects-running-method-of-derived-class – Sam Varshavchik Jan 25 '21 at 21:34