You are here:

C++/Error: Vector Subscript out of range

Advertisement


Question
Hi,

I am declaring 2 vectors as
vector<int> abc;
vector<int> xyz;

i am passing the vector "xyz" as an argument to a function and the return value is stored in vector "abc". But it shows a run-time error specifying that
"Vector Subscript out of range".

what should I do in order to get out of this run time error.

Please help.

Thanks!!

Answer
Hello Rishi, thank you for the question.

Unfortunately, there is no way to deduce this runtime error with actually seeing your code. Can you please post it?

This is most likely caused by accessing out of bounds of the vector.

Here is an example:

vector<int> testVec;
testVec.push_back(0);
testVec.push_back(1);

std::cout << testVec[3];

This goes outside the bounds of the vector at runtime.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.