Q: instantiating c++ Vector with different objects It's been a couple years since I've touched C++ and I'm having a bit of difficulty with what should be an easy task. I have a Vector of Object objects. the Object constructor takes in X and Y coordinates. For each Object in the Vector I'd like to instantiate it with psuedo-random coordinates. However, I cannot seem to make the code below work for me. Any help would be appreciated.
Image::Image(unsigned nObjects)
{
  srand(2);
  this->nObjects = nObjects;
  objects.resize(nObjects);

  for(int i = 0; i < nObjects; ++i)
  {
      objects.insert(new Object(rand(), rand(), rand(), rand()));
  }
}

Here's how the vector is instantiated in the Image header file:
std::vector<Object> objects;


A: new returns a pointer, you have a vector of Object values, not pointers to Object. You don't need to allocate your Objects explicitly on the heap (unless you will be using Object polymorphically), add items to the vector with
objects.push_back(Object(rand(), rand(), rand(), rand()));

If objects is meant to be a collection of derived instances you will need to use something like the following
std::vector<std::unique_ptr<Object>> objects;

and add items with
objects.push_back(std::make_unique<Some_derived_class>(rand(), rand(), rand(), rand()));

(if your library supports make_unique, otherwise roll your own).
If your library doesn't support unique_ptr then update to something that does.

A: objects.reserve(nObjects);
for(int i = 0; i < nObjects; ++i)
    objects.emplace_back(rand(), rand(), rand(), rand());

note that you cant both resize and do push_back or emplace_back
