C++类成员变量在循环后重置?
在获得有关这个问题的帮助后,我被引导进行更好的调试。在此过程中,我发现我的问题是这样的:
在 C++ 中工作时,尝试将类的成员变量设置为值是可行的,但在循环时则不行。我已将我的代码(如下)减少到我认为最简单的代码,因为仍然会产生错误。
调用类 Mover 的函数来修改变量 pMovXPOS,然后可以在同一范围内(在该函数内)并从调用它的位置(在循环内)检索更新后的变量。然而,在循环时,变量似乎被重置为其原始值。
我在这里发布了完整的测试代码。问题出在 Main-test.cpp 文件的 RunWorld() 函数中。如果编译并运行,您应该看到显示变量更改然后被重置的输出。
这是范围问题吗?建设/破坏问题?指针/引用问题?我不知道从哪里开始(除了更好的调试)。
(由于我是 C++ 新手,我确信我使用的样式和/或方法存在一些明显的问题。如果有任何主要禁忌,请随意指出。)
提前感谢您的任何建议帮助!
//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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是这一行:
您正在创建位于索引
iMoverLoop
处的Mover
的副本,然后修改该副本。向量中的对象永远不会被修改,并且在下一次迭代中您的更改将会丢失,因为TempMover
会被AllMovers
中的下一个副本覆盖解决此问题的一种方法将使用
TempMover
的引用来代替。例如:Your problem is this line:
You're creating a copy of the
Mover
that is at the indexiMoverLoop
, then modifying that copy. The object that is in the vector is never modified and on the next iteration your changes are lost, asTempMover
is overwritten with the next copy fromAllMovers
One way to get around this would be to use a reference for
TempMover
instead. For example: