In C++, I declare a custom class to store some values for an object. Then, I declare a vector of said object. Finally, I iterate through the vector to assign values to the fields.
#include <vector>
using namespace std;
class Custom
{ 
    public:
        int metric,nX,nY;
    private:
};
int main( int argc, char** argv )
{
vector<Custom> MyWonderfulVector;
// Some code//
for(int i=0 ; i<10 ; i++){
MyWonderfulVector[i].metric = computation1();
MyWonderfulVector[i].nX= computation2();
MyWonderfulVector[i].nY= computation3();
}
return 0;
}
It throws a vector subscript out of range when it tries to evaluate MyWonderfulVector[i].metric = computation1();. metric is an int and computation1() too. at the first iteration, i=0 so it should be ok. Curiously, somewhere else in the code, I have a vector of another class (included in a library) and this syntax works for it, so I don't understand why it doesn't work here.
EDIT :
Ok with the comments I changed to following line: vector MyWonderfulVector(10);
So my problem is that I did not initialize the size of the vector (bad habit from Matlab ;) ) From what I understand, if I don't initialize the vector's to a fixed size, I must push_back the objects to "increase" the size of the vector. So, I should create a temporary Custom Object to assign the fields, then push_back this temp object into the vector. If one of the commenter wants to put this into an answer...
You declare a vector of Customs in the line
   vector<Custom> MyWonderfulVector;
but it is an empty vector. There are no items in it. When you try to access the elements of the vector in the for loop, you are accessing the vector using out of bounds indices.
I can think of the following options for fixing that problem.
Create the vector with an initial size.
 vector<Custom> MyWonderfulVector(10);
Add to the vector in the for loop.
for(int i=0 ; i<10 ; i++){
  Custom c;
  c.metric = computation1();
  c.nX= computation2();
  c.nY= computation3();
  MyWonderfulVector.push_back(c);
}
You defined a vector with no elements
vector<Custom> MyWonderfulVector;
If you call its member function empty like
std::cout << std::boolalpha << MyWonderfulVector.empty() << std::endl;
then you will get true
So you may not use the subscript operator applied to an ampty vector except with index 0 but in any case you may not assign a value.
You could either define the vector initially with some_variable  elements like
vector<Custom> MyWonderfulVector( some_variable );
and then you could use your loop. Or you could reserve space for some_variable  elements in the vector and in this case use member function push_back instead of the subscript operator. For example
vector<Custom> MyWonderfulVector;
MyWonderfulVector.reserve( some_variable );
for ( int i=0 ; i<some_variable ; i++ )
{
    Custom obj;
    obj.metric = computation1();
    obj.nX= computation2();
    obj.nY= computation3();
    MyWonderfulVector.push_back( obj );
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With