C ++类成员变量重置循环后? [英] C++ Class Member Variables Resetting After Loop?

查看:191
本文介绍了C ++类成员变量重置循环后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在获取有关此问题的帮助后,我被带领做了更好的调试。在这个过程中,我发现我的问题是这样的:

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

在C ++中工作时,尝试将一个类的成员变量设置为值是有效的,但不是循环。我已经减少了我的代码(以跟随),我相信是最简单的仍然产生的错误。

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.

调用类Mover的函数修改变量pMovXPOS ,然后可以在同一范围内(在该函数内)和从它被调用的位置(在循环内)检索更新。但是,在循环时,似乎该变量被重置为其原始值。

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.

我在这里发布整个测试代码。问题在于Main-test.cpp文件的RunWorld()函数。如果你编译和运行,你应该看到显示变量改变,然后被重置的输出。

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).

(因为我是新的C ++,我敢肯定有一些明显的问题,或

(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.)

感谢您提供任何帮助!

//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


推荐答案

您的问题是这一行:

TempMover = AllMovers.at(iMoverLoop);

您正在创建 Mover 处于索引 iMoverLoop ,然后修改该副本。向量中的对象从不修改,在下一次迭代中,您的更改将丢失,因为 TempMover 将被下一个副本覆盖 AllMovers

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

一种解决方法是改用 TempMover 的引用。例如:

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

Mover& tempMover = AllMovers.at(iMoverLoop);
tempMover.DoMove();

这篇关于C ++类成员变量重置循环后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆