1

I'm new to C++ from a java/python/etc background, and am trying to teach myself OO programming before I have to take a class on it next semester.

I'm trying to make an animation system using SFML, but am having some trouble with one of my class variables; it keeps resetting to 0 after I increment it. I'll start with the code and follow with a log output I'm using to help figure out what's going on.

SOLUTION: Being new to C++, I was an idiot and returned a new instance of my class in my getter functions; using [class]& func()... instead of [class] func() solved this, but now I have some refactoring to do.

Code (header):

...

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);
}

Code (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;
}

So, my log output looks good since I'm testing with 2 frames on limbs 6 & 8, except those limbs' curFrame seems to reset to 0 after incrementing:

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

Edit: Code calling the increment function. (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;
}

getLimbFrame

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];
    }
}
David G
  • 94,763
  • 41
  • 167
  • 253
Will
  • 380
  • 4
  • 14

2 Answers2

4

Hopefully you're aware that your functions take arguments by value, so they work on a copy of something.

You carefully avoided to show the really interesting code parts where you call incrementAnimation, most likely it follows the same bad pattern as the other functions.

I suggest reading up on how to pass objects by reference and const reference -- and how function arguments work in C++.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
  • But curFrame is a class variable that doesn't get passed into or returned from any of the functions I'm using here, so it should remain consistent throughout the instance, right? – Will Jun 27 '13 at 00:49
  • 1
    throughout the *same* instance, but we have no proof of that yet – Balog Pal Jun 27 '13 at 00:50
  • I've edited my post to add the code which calls the increment function - I don't think that it defines new things every iteration. Thanks for the help. – Will Jun 27 '13 at 00:57
  • 1
    `getLimbFrame()` is still invisible – Balog Pal Jun 27 '13 at 01:00
  • Oops, added the relevant code for that. I'd put in loadAnim(), but it's long and seems to work as intended since the numFrames variables remain correct. – Will Jun 27 '13 at 01:04
  • 1
    there we go: getLimbFrame() returns a new limbAnim instance (that is a copy of limbFrames[index]) every time you call it. – Balog Pal Jun 27 '13 at 01:04
  • Ah, thanks - so should I make the vector public or is there a way to just return the value? I'm still not too clear on where to use '*' and '&'. I know it's not pretty or particularly good practice, but that's what I'm trying to learn. – Will Jun 27 '13 at 01:06
  • 1
    Just to try that this problem gets cured change it to return limbAnim&. But before doing anything serious you really need to learn those nuances adding const in the mix, otherwise you'll keep getting errors and end up with some dangerous code – Balog Pal Jun 27 '13 at 01:09
  • That did it, thanks for the help. Like I said, I'm mostly just trying to prototype some fun things to learn C++, but I am definitely going at it as someone used to interpreted languages; any reading you could recommend on getting used to OO from an interpreted background? – Will Jun 27 '13 at 01:12
  • 1
    http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list is the book list, IMO Accelerated C++ is still the most recommended beginner book – Balog Pal Jun 27 '13 at 01:20
1

I think you need to declare that member variable, with the static keyword, then you can say it is a class variable, where it will be the shared for every instance of your class. Like this:

static int curFrame;

Then you need to initialize it from outside de class. Have in mind that declaration, is a lot different to initialization.

You can read about it here and here

  • I don't want to make it static though, because there are several instances of the class which need to have separate values of the variable. – Will Jun 27 '13 at 00:58
  • 1
    Yeah, I just realized that reading your comments on the other answer. I'm trying to spot the mistake on your code, but I see none so far – Raziel Ravenheart Jun 27 '13 at 01:01
  • 1
    @Will when you call `incCurFrame();` inside `limbAnim::incrementAnimation`, what's the value of `numFrames`? – Raziel Ravenheart Jun 27 '13 at 01:05
  • 1
    It's 2 on the relevant limbs, which is what it should be. It looks like the issue is that my getLimbFrame method is returning a new instance every time it's called. – Will Jun 27 '13 at 01:09