0
struct Books{

std::string title;
std::string author;
std::string des;
int book_id = 0;
char identifier;

} ;

// book 0
book[0].title = "Programming Fundamentals"; // subscript out of range
book[0].author, "Robert Hanks";
book[0].des, "Programming Basics";
book[0].book_id = 101;
book[0].identifier = 'P';

// Struct Object
std::vector <Books> book;

When ever I try to compile the above code its giving me a subscript out of range error.

Am I doing something wrong?

Thanks.

1 Answer 1

1

you have created an empty vector and trying to access it's elements by book[0] statement, that is not correct. you need to have atleast one element in the vector before accessing it using book[0].

Initialize your vector to have atleast one element in the vector. I am giving one example below to fix it.

  // Struct Object
  std::vector <Books> book(1);

  // book 0
  book[0].title = "Programming Fundamentals"; // subscript out of range
  ....
  ....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.