Iterating through a vector of pointers to class objects

Hello,

Essentially, this test program just creates a number of objects with different (x,y) positions and varying energy levels. Then I decrease each object's energy, and I want to be able to delete it if the energy level has reached zero.

The creation of the objects, and their placement into a vector of pointers works perfectly, but I cannot seem to access the members when I try to iterate through it, and I'm not really sure what's wrong with the syntax. Any help is appreciated...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <vector>

using namespace std;

int xMax = 5;
int xMin = 0;
int yMax = 5;
int yMin = 0;

class Object {
      int xPos;
      int yPos;
      int energy;
      int id;
      
      public:
       static int numObjects;
       
       Object(int j, int i, int amount);
       ~Object();
       
       void increaseEnergy(int amount) {energy += amount;}
       void decreaseEnergy(int amount) {energy -= amount;}
       
       void move_right() {if (xPos < xMax-1) ++xPos;}
       void move_left()  {if (xPos > xMin+1) --xPos;}
       void move_up()    {if (yPos < yMax-1) ++yPos;}
       void move_down()  {if (yPos > yMin+1) --xPos;}
};

Object::Object(int j, int i, int amount) {
     ++numObjects;
     xPos = j;
     yPos = i;
     energy = amount;
     id = numObjects;
     
     cout << "Creating Object "  << id << ":\n\n";
     cout << "xPos =   " << xPos       << "\n";
     cout << "yPos =   " << yPos       << "\n";
     cout << "energy = " << energy     << "\n\n"; 
}

Object::~Object() {
     cout << "\nDestructing Object " << id << ".\n\n";
}

int Object::numObjects = 0;

int main() {
    
    vector<Object*> objects;
    vector<Object*>::iterator iter;
    
    for (int i=yMin; i<yMax; ++i) {
        for (int j=xMin; j<xMax; ++j) {
            Object* newObj = new Object(j,i,i*j);
            objects.push_back(newObj);
        }
    }
    
    cout << "\n\n---------------------------------------\n\n";
    

    // Here's where my problem exists:

    for (iter = objects.begin(); iter != objects.end(); ++iter) {
        (*iter).decreaseEnergy(10);
        //iter->decreaseEnergy(10);
        if ( (*iter).energy <= 0) delete (*iter); //this statment might be way off, too.
    }
    
    cin.get();
    return 0;
}
You should be using line 70, not 69. but as (*iter)->dec etc

Line 71 is correct (but use -> not .). But then you must also remove that pointer from your vector using vector.erase(iter). The delete is only freeing the memory.

Remember that after you do an erase, the iterator is moved to the next object in the vector. Thats why using a for-loop is bad, your iter will be incremented and skip the next one.
Last edited on
Thanks. I guess I forgot to consider that the iterator is itself basically a pointer. As for the "for" loop, it's an easy problem to solve: in the "if " statement, all I have to do is decrement the iterator and no index will be missed.
yes. But you're better off using a while loop
1
2
3
4
5
while (vPtr != vector.end()) {
 if blah continue;

 vPtr++;
}
Last edited on
Topic archived. No new replies allowed.