473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Having problems with vectors!

3
Hello.

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
2 1721
Banfa
9,065 Expert Mod 8TB
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

myStudent.resize(count);

to set the vector to a give size with each object taking it's default value.
Sep 30 '06 #2
l46kok
3
Thanks! it works
Oct 1 '06 #3

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

Similar topics

12
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
17
by: Gernot Frisch | last post by:
Hi, does repeatingly doing this: float num = GetRandomFloat(); for(;;) { float random = GetRandomFloat(); num*=random; num/=random;
11
by: sw | last post by:
Hi, Is it possible to insert a class <vec> which has a vector<double> member, into the vector<vec> veclist for e.g? I've been getting compilation errors when trying to insert using the vector...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
2
by: wuzertheloser | last post by:
Use the program skeleton below (starting with #include <stdio.h>) as the starting point for quiz4. Add the necessary code to the functions prob1() and prob2(), and add the other 2 functions, as...
1
japuentem
by: japuentem | last post by:
I have an application that receive some vectors inside one main vector. The user set the information to the arrays vector1 {a, b} vector2 {1, 2} vector3 {x, y} vector4 {20,21} vector5 {s,t}...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
2
by: joeme | last post by:
How would one using STL do the following tasks: 1) merge 2 sorted vectors with dupes, result shall be sorted 2) merge 2 sorted vectors without dupes, result shall be sorted 3) merge 2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.