Help with vectors

I am having an issue with vectors and I can't seem to find the answer in the documentation on vectors. I made a class with functions inside and then added that class into a vector. However, I am not even sure if that is the right approach. From what I understand, when you put a class into a vector it allows for the class to be reused. Here is what I have in a nutshell.

header
1
2
3
4
5
6
7
class ClassName{
public:
functions

private:
std::string name;
}


source
1
2
3
4
5
6
7
8
includes

ClassName::definitions

int main()
{
vector< ClassName > classNameVector;
}


When I compile the program, there are no errors with the definition of the vector. The errors occur when I try and do anything with the vector. An example is taking input from the user and applying it to the name string inside of the class.
How do you use it?
Does object, you trying to get acces to, exist?
Last edited on
Here's an example.

1
2
3
4
5
for( int i = 0; sizeOfVector > i; i++ )
{
vectorNumber = i + 1;
cout << vectorNumber << ". " << classNameVector[ sizeOfVector ].getValue();
}


1
2
3
4
5
classNameVector.resize( sizeOfVector + 1 );
cout << "Please enter a name in the form Last, First Middle:\n";
cin.ignore();
getline(cin, changedName);
classNameVector[ sizeOfVector ].changeName( changedName );


So what I want to happen is the bottom code to run first The user is essentially creating a new entry (expanding the vector by 1) and enters a name. This passes the string to the function changeName (not shown but it is expecting a string atgument) which is in the class. That seems to go okay. Then the user has the option to display what was entered by using the top code which checks the size of the vector and prints a list in numbered form with the data. I ran a debug console to find out where it is crashing and I got the error
1
2
3
"#0 0044C184	std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) () (??:??)
#1 00401D7C	check_exception_spec(lsda_header_info*, std::type_info const*, void*, long) () (??:??)
#2 0028FDA8	?? () (??:??)"


This is after the first block of code runs.
classNameVector[ sizeOfVector ]. <- Out of bound access here.
Maybe you want classNameVector[ i ].?
That's what i was thinking too. I initially had "i" in there and it came up with some error I cant remember. Ill try using "i" again and see how it goes.
Topic archived. No new replies allowed.