I have a class A, which has a static vector of objects. The objects are of class B
class A {
public:
static void InstantiateVector();
private:
static vector<B> vector_of_B;
}
In function InstantiateVector()
for (i=0; i < 5; i++) {
B b = B();
vector<B>.push_back(b);
}
But I have compilation error using visual studio 2008: unresolved external symbol…
Is it possible to instantiate static vector using above method? For object b to be created, some data has to be read from input file, and stored as member variables of b
Or it is not possible, and only simple static vector is possible? I read somewhere that to instantiate static vector, you must first define a const int a[] = {1,2,3}, and then copy a[] into vector
You have to provide the definition of
vector_of_bas follows:As a side note, your
InstantiateVector()makes a lot of unnecessary copies that may (or may not) be optimized away.In fact, for this simple example where you are just default constructing
Bobjects, the most concise way of doing this is simply to replace the loop all together with: