I am having some issues creating a 2-d vector.
I need it to be of a determined length in the first dimension but of an undetermined length in the other. The length in the second dimention will be calculated later in the code, in one of the functions that uses the vector and writes to it.
So, here is what I have:
string line ("line");
string circle ("circle");
string sphere ("sphere");
string addparticle ("particle");
int NumberObjects;
int number_line = 0;
int number_circle = 0;
int number_sphere = 0;
int number_particle = 0;
cout<< "How Many Objects Would You Like To Add?" << endl;
cin >> NumberObjects;
vector<string> shape(NumberObjects);
for (int i = 0; i<NumberObjects; i++)
{
cout << "Shape " << i << " ? (line / circle/ sphere / particle)" << endl;
cin >> shape[i];
if( shape[i].compare(line) == 0)
{
cout << shape[i] << " Success!"<< endl;
number_line ++;
}
if( shape[i].compare(circle) == 0)
{
cout << shape[i] << " Success!"<< endl;
number_circle ++;
}
if( shape[i].compare(sphere) == 0)
{
cout << shape[i] << " Success!"<< endl;
number_sphere ++;
}
if( shape[i].compare(addparticle) == 0)
{
cout << shape[i] << " Success!"<< endl;
number_particle ++;
}
else
{
cout << shape[i] << " FAIL" << endl;
}
vector<vector<particle>> pline(number_line, vector<particle>());
vector<vector<particle>> pcircle(number_circle);
vector<vector<particle>> psphere(number_sphere);
vector<particle> pparticle(number_particle);
for (int i = 0; i < number_line; i++)
{
particle::CreateLine(pline[i]);
}
All the individual functions work and have been tested. The code complies, but then on running kick up an error saying "vector subscript out of range". Which I assume means that I am simply defining my vectors incorrectly.
Any help with this would be great, I have searched around but cannot find anything on defining a vector with an undetermined length.
If you require more code to explain the answer I can provide it, but I suspect it is quite a simple problem that either has an explanation or it cannot be done.
If you cannot define a 2-D vector where one of the dimensions is of an unspecified length could you point me in the direction of something that would do roughly the same thing? Contain a 2-d matrix of objects (particle in this case).
Just noticed something, the first definition of a vector I made is different to the others, this was me experimenting to try and find something that works. Suffice to say it did not, but if it is the same as all the others it does not work either.
Cheers
Josh