In order to create an array of objects, the objects need a constructor that doesn't take any paramters (that creates a default form of the object, eg. with both strings empty). This is what the error message means. The compiler automatically generates a constructor which creates an empty object unless there are any other constructors.
If it makes sense for the array elements to be created empty (in which case the members acquire their default values, in this case, empty strings), you should:
-Write an empty constructor:
class name {
public:
string first;
string last;
name() { }
name(string a, string b){
first = a;
last = b;
}
};
-Or, if you don't need it, remove the existing constructor.
If an "empty" version of your class makes no sense, there is no good solution to provide initialisation paramters to all the elements of the array at compile time. You can:
init()
function which does the real initialisationvector
, and on initialisation create the objects and insert them into the vector
, either using vector::insert
or a loop, and trust that not doing it at compile time doesn't matter.std::vector<std::string> v = { "xyzzy", "plugh", "abracadabra" };
`