I am learning to use classes and i want to create a vector of classes.
i can create the class and the vector of the class okay, but i cant seem to add to the vector using push_back.
A vector is similar to an array. So you have to access the vector elements like you would an array.
Your line std::cout << bob.health[0]; right idea, but wrong order. What you want is std::cout << bob[0].health;. "bob" is the vector with elements like an array. "health" is a member variable of the class.
My understanding of classes is the the variables should be "private" so that only the "public" functions of the class can access them. As you have it the whole program can access these variables. To me this defeats the purpose of the class.
Not sure what you intended for line 20, but it does not work.
The default ctor does nothing to initialize your variables or give them any values and there is no overloaded ctor to work with.
Line 20 either needs to push back an object of the class or the class needs an overloaded ctor to handle the numbers.
thank you for the responses everybody <3
furry guy's answer got it to work (although kudos to handy andy, i did also make a mistake on line 21!), but i see from the responses i still have much learning to do.
i am going to read through all the replies carefully and learn to fully understand what is going on. i think i may have jumped ahead of my learning a little and need to slow down :)
Enoizat brings up a very good point about how to access a vector's elements. operator [] vs. at.
operator[] does no bounds checking, it is very easy to go out of bounds. atdoes perform bounds checking, and if the program tries to access an element the vector does not own an exception is thrown (the program crashes if you don't try/catch).
#include <iostream>
#include <vector>
int main()
{
std::vector<int> my_vec { 34, 56, -21, 5002, 365 };
std::cout << "1st element at index 0: " << my_vec[0] << '\n';
std::cout << "2nd element at index 1: " << my_vec.at(1) << '\n';
std::cout << "3rd element at index 2: " << my_vec[2] << '\n';
std::cout << "4th element at index 3: " << my_vec.at(3) << '\n';
std::cout << "5th element at index 4: " << my_vec[4] << "\n\n";
// one of the most common errors when dealing with vectors is accessing past the last element
std::cout << "6th element at index 5: " << my_vec[5] << "\n\n"; // out of bounds!
// std::cout << my_vec.at(5) << '\n'; // crashes!
std::cout << "The first element is: " << my_vec.front() << '\n';
std::cout << "The last element is: " << my_vec.back() << '\n';
}
1st element at index 0: 34
2nd element at index 1: 56
3rd element at index 2: -21
4th element at index 3: 5002
5th element at index 4: 365
6th element at index 5: 11688672
The first element is: 34
The last element is: 365
Uncomment line 18 and watch the program fall down and go *BOOM!*
thanks for the info.
i have much learning ahead of me!
to be honest im not sure im going about this the best way anyway. but i am learning a lot from doing it wrong :) and from the people here on this forum <3