gpt4 book ai didi

c++ - 类变量在函数完成后重置

转载 作者:行者123 更新时间:2023-11-30 01:21:55 25 4
gpt4 key购买 nike

我是 C++ 新手,有 java/python/etc 背景,我想在下学期上课之前自学 OO 编程。

我正在尝试使用 SFML 制作一个动画系统,但我在使用我的一个类变量时遇到了一些问题;在我增加它之后它会一直重置为 0。我将从代码开始,然后是我用来帮助弄清楚发生了什么的日志输出。

解决方案: 作为 C++ 的新手,我是个白痴,在我的 getter 函数中返回了我的类的一个新实例;使用 [class]& func()... 而不是 [class] func() 解决了这个问题,但现在我需要进行一些重构。

代码(标题):

...

typedef std::vector<Frame> frameVect; // (Frame defined above)
typedef std::vector<double> dubVect;

...

class limbAnim
{
private:
int limbNum;
int numFrames;
int curFrame;
frameVect frames;

public:
limbAnim(int limb, int nFrames, frameVect F);
<getters/setters>
void incCurFrame();

dubVect incrementAnimation(dubVect curPos, double curRot);
}

代码(cpp):

... (include vector, ofstream, etc)

std::ofstream AnimLog("log.log")

typedef std::vector<Frame> frameVect; // (Frame defined above)
typedef std::vector<double> dubVect;

...

limbAnim::limbAnim(int limb, int nFrames, frameVect F)
{
limbNum = limb;
curFrame = 0;
numFrames = nFrames;
frames = F;
}

void limbAnim::incCurFrame()
{
curFrame=curFrame+1;
if (curFrame >= numFrames)
{
curFrame = 0;
AnimLog << "Greater than." << std::endl;
}
}

dubVect limbAnim::incrementAnimation(dubVect curPos, double curRot)
{
AnimLog << limbNum << ", " << numFrames << std::endl;

if (numFrames > 0)
{
AnimLog << curFrame << std::endl;
dubVect curStepP = frames[curFrame].getStepPos();
double curStepR = frames[curFrame].getStepRot();

curPos[0] = curPos[0] + curStepP[0];
curPos[1] = curPos[1] + curStepP[1];

curRot = curRot + curStepR;

incCurFrame();
AnimLog << "Incremented: " << curFrame << std::endl;
}

dubVect retV = curPos;
retV.push_back(curRot);
return retV;
}

因此,我的日志输出看起来不错,因为我在肢体 6 和 8 上测试了 2 帧,除了那些肢体的 curFrame 在递增后似乎重置为 0:

...
5, 0
6, 2
0
Incremented: 1
7, 0
8, 2
0
Incremented: 1
9, 0
...
5, 0
6, 2
0
Incremented: 1
7, 0
8, 2
0
Incremented: 1
9, 0
...(ad nauseam)

编辑:调用增量函数的代码。(main.cpp)

// (Outside main loop.)
Animation walk_anim(12, "assets/anim/walk.dat");

// (Inside main loop.)
for (int i=0; i<12; i++)
{
dubVect animDat = walk_anim.getLimbFrame(i).incrementAnimation(limbPos[i], curDegs[i]);
dubVect newPos = getDVect(animDat[0], animDat[1]);
double newRot = animDat[2];
curDegs[i] = newRot;
if (curDegs[i] >= 360)
curDegs[i] -=360;
limbPos[i] = newPos;
}

获取肢体框架

Animation::Animation(int lNum, string fName)
{
numLimbs = lNum;
fileName = fName;

// Fill up limbVect with correct # of empty frames.
for (int i=0; i<numLimbs; i++)
{
frameVect emptyFVect;
limbAnim LA(i, 0, emptyFVect);
limbFrames.push_back(LA);
}

// Boring .dat parsing, populates the 'frames' var of each limbAnim.
loadAnim();
}

limbAnim Animation::getLimbFrame(int index)
{
if (index < numLimbs)
{
return limbFrames[index];
}
}

最佳答案

希望您知道您的函数按值接收参数,因此它们在某些东西的拷贝 上工作。

您小心翼翼地避免显示调用 incrementAnimation 的真正有趣的代码部分,很可能它遵循与其他函数相同的错误模式。

我建议阅读如何通过引用和 const 引用传递对象——以及函数参数在 C++ 中的工作方式。

关于c++ - 类变量在函数完成后重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17332713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com