How do I construct a vector of objects that have constructor arguments?
c++, vector
Solution
The code you posted is fundamentally fine, bar a few typos and missing includes. You can see this working demo.
But you don't need to create the temporary `Circle` object. You can do this:
for (int i = 0; i < 10; i++) {
circlesVector.push_back(i);
}
because `Circle` is implicitly constructable from `int`. Note that you can also use an initializer list:
vector<Circle> circlesVector{0,1,2,3,4,5,6,7,8,9};
alternatively, use `emplace_back`:
circlesVector.emplace_back(i);
Problem
I want to do something like: ``` class Circle { int radius; public: Circle( int r ) : radius(r) {} } vector<Circle> circlesVector; for (int i = 0; i < 10; i++) { Circle circle(i); circlesVector.push_back(circle); } ``` But this does not seem to work the way I want it to. The vector tries creating Circle objects using a constructor for Circle(Circle&) or some behavior that I don't understand or expect.