question about vector

this is a question about vector object and it just a general question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
include "example.h"
class : libaray
  example 1
  //let say you have a privates variable
  private:
    unsigned int numBook
    example books// this is just an example
  // you make the private variable into a vector 
    private:
    std::vector<example> books
    //why does a vector get an error when you do books = new example[assume it fill in] 
    //would you need a for loop
    //why do vectors get this error book[assume it fill in] != nullptr

   
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
this is example i havent done any coding just some example i want to understand why they are errors

so how would you do the

books =new examples[assume it fill in]

with push.back().
Hello ghost1111,

You have defined books as a private member variable of a class. First you would need a public member function to access the private variable. Then since "example" appears to be class all the member variables of that class would have to be filled in. Then using the variable name for the "example" class the line of code would be books.push_back(varName);. Where "varName" would be defined as example varName;. And where "varName" is whatever name you want to use.

And this books =new examples[assume it fill in]. I think you should give up on trying to make this work. In the last year I have never seen anything that looks like this being used with a vector.

Based on the little bit of code you have shown it is hard for me to understand what you are trying to do or what you are trying to learn.

You may find these links of some use:

http://www.cplusplus.com/reference/vector/vector/
https://www.cprogramming.com/tutorial/stl/vector.html
https://www.google.com/search?safe=strict&source=hp&q=c%2B%2B+tutorial+vectors&oq=c%2B%2B+tutorial+vec&gs_l=psy-ab.1.0.0j0i22i30k1.2159.42678.0.46112.50.38.0.0.0.0.201.4214.6j27j1.35.0....0...1.1.64.psy-ab..17.32.4003.0..0i131k1j0i10k1j0i7i30k1.89.BpX8Vo1t6cE

If I had better idea of what you are trying to do it would be easier to explain/

Hope that helps,

Andy
Topic archived. No new replies allowed.