Hello ghost1111,
Without seeing "exmple.h" it is hard to follow what you have posted. It looks like you are trying to define a class, but it is wrong.
Line 10 is defined correctly as long as "example" is a class that I think it is, then each element added to a vector would be an entire class.
why does a vector get an error when you do books = new example[assume it fill in] |
"books" is a vector and as a vector it will handle getting storage space and keeping track of where everything is stored. You trying to use "new" on a vector does not work because it is not meant to. You do not have to write code to reserve space for a vector element. The vector member functions "push_back()" and what I see used more often today "emplace_back()" will get space to store what is being stored.
would you need a for loop |
No, but loops, for, do/while and while, do come in handy if you have more than one piece of something to work with, be it input or output.
why do vectors get this error book[assume it fill in] != nullptr |
Although you can use a subscript to access a vector it does not mean that each element of the vector would be something that you could check against a "nullptr". Vectors do not use a "nullptr" to mark the end of the vector. Most of the time you use "vectorName.size()" to know how many elements are in the vector. For instance a for loop might look like this
for (size_t lc = 0; lc < books.size(); lc++)
. I used "size_t" because ".size()" will return a "size_t". It just makes it easier when things match.
Once you understand vectors they are much nicer to use than arrays even though they are very similar to arrays.
Hope that helps,
Andy