简体   繁体   中英

Iterate through vector of objects c++

I've been struggling with this problem so I hope someone can help me.

I am creating a vector of objects and I would like to access elements of this vector but when I push back en element into the vector and try to access it, it doesn't work. Thank you for your answers.

class reserve{
  private:
      vector<float> coordinates; 
  public:
      reserve(const reserve &);
      void addACoordinate(float);
      void readCoordinates();
      int getDim();
};

reserve::reserve(const reserve &w){
    int i;
    for(i=0; i<coordinates.size(); i++){
        coordinates[i] = w.coordinates[i];
    }
}

void reserve::addACoordinate(float c){
    coordinates.push_back(c);
}

void reserve::readCoordinates(){

    int i; 
    cout << "the coordinates are : ";
    for(i=0; i<coordinates.size(); i++){
       cout << coordinates[i] << " ";
    }

    cout << endl;
}

int reserve::getDim(){
    return coordinates.size();
}

Then I create a reserve with 2 coordinates and push it into the reserves vector.

vector<reserve> reserves;
reserve r;

r.addACoordinate(1.9);
r.addACoordinate(1.9);
r.getDim();
reserves.push_back(r);
cout << "size of reserves " << reserves.size() << endl;


for (vector<reserve>::iterator k = reserves.begin(); k != reserves.end(); ++k) {
      k->getDim();

 }

But the output of the iteration is 0, instead of 2. I don't understand why I am not accessing the reserve r.

The shown copy constructor is completely broken:

reserve::reserve(const reserve &w){
    int i;
    for(i=0; i<coordinates.size(); i++){

The newly-constructed coordinates class member is always completely empty when the copy-constructor runs. It obviously won't magically have any content right from the start, so coordinates.size() will always be zero here. The copy constructor will never actually copy the element that's being used to copy from, and the alleged "copy" of it will always have an empty vector. And changing this to w.coordinates.size() will result in memory corruption, unless you also replace the assignment in the loop with a push_back() . Speaking of push_back ()...

reserves.push_back(r);

push_back() attempts to copy r into the vector, here. That's it's job. Guess what's going to be used to do that? Why, the broken copy constructor, of course. Even if r has an initialized coordinates vector, the broken copy constructor will ensure that reserves will end up with a class instance with a completely empty coordinates vector.

You can completely get rid of the copy constructor, here. It serves no useful purpose, and the default copy constructor will do everything correctly.

If you need to manually implement a copy constructor for your homework assignment, fix it so it properly push_back() s the values from the class instance being copied from, or manually copies the class members itself, whatever you need to do the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM