Reputation: 573
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...
Upvotes: 10
Views: 31323
Reputation: 206607
You declare a vector
of Custom
s 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);
}
Upvotes: 9
Reputation: 25
Try using resize() before accesing an element. So your code will become:
for(int i=0 ; i<10 ; i++){
MyWonderfulVector.resize(i);
MyWonderfulVector[i].metric = computation1();
MyWonderfulVector[i].nX= computation2();
MyWonderfulVector[i].nY= computation3();
}
If you further want to add some other elements, you can store the size of the vector in size_t variable, and increment it every time you want to add other element.
size_t my_vector_size = MyWonderfulVector.size();
for(int i=0 ; i<10 ; i++)
{
my_vector_size++;
MyWonderfulVector.resize(my_vector_size);
}
This is a way to do it.
Upvotes: 0
Reputation: 310990
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 );
}
Upvotes: 10
Reputation: 1212
I got the same problem before. I tried using the push_back function from vector class and it worked. Maybe it will solve your problem
Upvotes: 2