1

I want to make an array of objects made from class, but i don't know how many of those objects will be in the array. I tried to make it with this code: MyClass a[];, but Qt Creator shows me an error: flexible array member 'a' of type 'MyClass []' with non-trivial destruction and a warning: flexible array members are a C99 feature. The code works with MyClass a[n]; but that is not what i need.

1
  • 9
    Try std::vector<MyClass> a;. Commented Dec 25, 2019 at 17:25

1 Answer 1

2

In C++ you can make usage of vector<MyClass>a. Whenever you need to add any more object to the array use: a.push_back(object)

Basically when you declare a vector, a fix capacity container is created dynamically. Whenever the size of vector crosses the capacity, the entire vector is copied to a new location with increased size.

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

Comments

Your Answer

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