Question

After getting help on this question, I was led to do better debugging. In that process, I discovered that my problem is this:

While working in C++, attempting to set a class' member variable to a value works, but not while looping. I have reduced my code (to follow) to what I believe is the simplest for that still produces the error.

Calling a function of the class Mover that modifies the variable pMovXPOS, which can then be retrieved as updated within the same scope (within that function) and from the where it was called (within the loop). However, upon looping, it seems the variable is reset to its original value.

I'm posting the entirety of the test code here. The problem lies in the RunWorld() function of the Main-test.cpp file. If you compile and run, you should see the output that shows the variable changing, then being reset.

Is this a scope issue? A construction/destruction issue? A pointer/reference issue? I'm not sure where to begin (beyond better debugging).

(As I am new to C++, I'm sure there are some glaring issues with style and/or methods I've used. If there are any major no-nos, please free to point those out.)

Thanks in advance for any help!

//Main-Test.cpp
#include "Globals-Test.h"
#include "Mover-Test.h"

std::vector < Mover > AllMovers;

long SysCounter;

Mover CreateNewMover() {
Mover TempMover;

    TempMover.setXPOS(5);
    TempMover.setYPOS(10);

    return TempMover;

}

void RunWorld() {
Mover TempMover;
unsigned int iMoverLoop;

    srand ( time(NULL) );

    AllMovers.push_back(CreateNewMover());

    for (SysCounter = 0; SysCounter <= 50; SysCounter++) {
        for (iMoverLoop = 0; iMoverLoop < AllMovers.size(); iMoverLoop++) {
            std::cout << "Loop #:" << SysCounter << std::endl;
            TempMover = AllMovers.at(iMoverLoop);
            std::cout << "Is: " << TempMover.getXPOS() << std::endl;
            TempMover.DoMove();             
            std::cout << "Is: " << TempMover.getXPOS() << std::endl;
        }
    }
}



int main() {
    RunWorld();
    return 0;
}

//Globals-Test.h
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <unistd.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <vector>
#include <time.h>
#include <fstream>

//Mover-Test.h
extern long MoverIndex;

class Mover {

private:

    int pMovXPOS;
    int pMovYPOS;

public:

    int getXPOS();
    void setXPOS(int newXPOS);
    int getYPOS();
    void setYPOS(int newYPOS);

    Mover();
    ~Mover();

    void DoMove();

};

//Mover-Test.cpp
#include "Globals-Test.h"
#include "Mover-Test.h"

Mover::Mover() {    

}

Mover::~Mover() {

}

int Mover::getXPOS() {
    return pMovXPOS;
}

void Mover::setXPOS(int newXPOS) {
    pMovXPOS = newXPOS;
}

int Mover::getYPOS() {
    return pMovYPOS;
}

void Mover::setYPOS(int newYPOS) {
    pMovYPOS = newYPOS;
}

void Mover::DoMove() {
pMovXPOS = pMovXPOS + 1;
pMovYPOS = pMovYPOS + 1;

}

//Compiled with:
g++ -Wall -lm -c Main-Test.cpp
g++ -Wall -lm -c Mover-Test.cpp
g++ -Wall Mover-Test.o Main-Test.o -o world-test.exe -lm
Was it helpful?

Solution

Your problem is this line:

TempMover = AllMovers.at(iMoverLoop);

You're creating a copy of the Mover that is at the index iMoverLoop, then modifying that copy. The object that is in the vector is never modified and on the next iteration your changes are lost, as TempMover is overwritten with the next copy from AllMovers

One way to get around this would be to use a reference for TempMover instead. For example:

Mover& tempMover = AllMovers.at(iMoverLoop);
tempMover.DoMove();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top