Updating members of std::list

Hello all,

I have a PointManager class which inserts Points (another class) into an std::list. Point insertion works just fine.

However, these "Points" contain, among other things, an x and y value which are often changing.

I'd like to be able to tell my PointManger to UpdateAll(), and have it iterate through the std::list, and set the position of each Point's sprite(sf::Sprite, from SFML) to that point's most recent x and y values.

As it is, I know that each point's Update() method is being called successfully from PointManager's UpdateAll(), but none of the changes taking place in Point::Update() are reflected in PointManager. If I print PointManager's contents, no x and y values have changed and the sprites on screen haven't moved.

Here is the code for PointManager::UpdateAll():

1
2
3
4
5
6
7
8
9
void PointManager::UpdateAll()      
{
    for(itr=PointList.begin(); itr != PointList.end(); ++itr)
    {
        Point temp = *itr;
        temp.Update();
        *itr = temp;
    }
}

and the code for Point::Update() although I don't think there's anything wrong with it:
1
2
3
4
5
void Point::Update()
{
    Sprite.SetPosition(_x,_y);
    std::cout << _id << " updated." << std::endl;
}

Any help will be met with eternal gratefulness and possibly sandwiches. I have been stuck on this for a solid 2 days now, and nothing I can think to google has solved my problem. Thank you in advance!
Last edited on
That code looks okay. Though it could be shortened to itr->Update();.
The problem could be with how you manage _x and _y values. Try changing your cout to
_id << " updating from " << Sprite.getPosition().x << ", " << Sprite.getPosition().y << " to " << _x << ", " << _y << '\n';.
Interesting. The problem must be somewhere else. Right now it's just outputting:

Point 1 updating from 0,0 to 0,0
Point 2 updating from 13,12 to 13,12
etc.

So no changes are being made. It may have to do with how I'm manipulating _x and _y, though I imagine my SetX() function in the Point class is fine... -_-

I will have to think about this some more. Thank you for verifying that this snippet of code should be working, though!

EDIT: FIXED IT! My problem was, after all, the method by which I was manipulating the Points. I was trying to explicitly change their values from my main() method, where I should have implemented a method in PointManager that iterates through, finds the right point, and changes _x and _y by itself. If that makes any sense. In any case, thank you again.
Last edited on
Topic archived. No new replies allowed.