By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

Having problems with vectors!

P: 3

This is the first time I'm encountering vectors and I'm having problems.

I want to create a vector filled with objects, but as soon as I try to invoke a

function within the object,

I get an error message stating "Vector out of subscript range". And I'm using

visual studio 2005.

So for example
int main()
vector<student> myStudent; // Vector filled with student objects
myStudent[0].enterGrade(200); // Compiler error

I must be missing something very simple yet I can't quite figure out. Any help would be appriciated!
Sep 30 '06 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,916
You never assign the vector a size and you never assign the vector any objects

vector<student> myStudent; // Vector filled with student objects
creates the vector

myStudent[0].enterGrade(200); // Compiler error
but this tries to access the student object at index [0] before any student object has been created at that index.

You can use

myStudent.push_back( <object> );

to add a specific object to the end of the vector or


to set the vector to a give size with each object taking it's default value.
Sep 30 '06 #2

P: 3
Thanks! it works
Oct 1 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.