Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:24:16+00:00 2026-05-30T04:24:16+00:00

After getting help on this question , I was led to do better debugging.

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:24:17+00:00Added an answer on May 30, 2026 at 4:24 am

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After getting fine answer to my previous question , I came across another problem.
In Reference to this question After getting the line identifier matching in first and
Hey guys. This is a follow-on from this question : After getting the right
After getting a helpful answer here , I have run into yet another problem:
Update: Ok, after getting past the fact that I did not have MSDTC set
I have designed one sign-up form,in this form after getting all necessary values I
Keep getting this error after inserting a subdatasheet into a query and trying to
After having solved the problem of getting the editor to launch when there's a
I admit this question is getting to the limits of what I know of
My answer: After getting annoyed, I have found a solution. The problem was indeed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.