Uninitialized reference member

c++

Solution

A reference cannot be reassigned, so it must be initialized at the member-initialization-list. However, you intend to reassign it so what you want is not a reference. What's more, in your `setActiveAnimation` function you are setting such reference to a copy of the value passed as an argument, which leaves you with an invalid reference when the code exits the function. Perhaps a pointer would suit you?

In the class body:

MaJR::Animation* activeAnimation;

And the `setActiveAnimation` function:

void Actor::setActiveAnimation(Animation* anim)
{
    activeAnimation = anim;
    activeAnimation->gotoStart();
}

Problem

Basically what I want to do is store the reference to the active animation of a sprite as a private member in the Actor class. I want to use a reference so I don't have to actually create the animation multiple times but I keep getting an error. Actor class declaration: ``` class Actor { public: Actor(); ~Actor(); void setActiveAnimation(Animation anim); void draw(sf::RenderWindow& win); private: sf::Sprite sprite; MaJR::Animation& activeAnimation; }; ``` Actor class implementation: ``` Actor::Actor() { // constructor } Actor::~Actor() { // destructor } void Actor::setActiveAnimation(Animation anim) { activeAnimation = anim; activeAnimation.gotoStart(); } void Actor::draw(sf::RenderWindow& win) { sprite.setTexture(activeAnimation.getActiveFrame()); win.draw(sprite); activeAnimation.nextFrame(); } ``` Build output: ``` /home/mike/MaJR Game Engine/src/Actor.cpp||In constructor 'MaJR::Actor::Actor()':| /home/mike/MaJR Game Engine/src/Actor.cpp|8|error: uninitialized reference member 'MaJR::Actor::activeAnimation' [-fpermissive]| ||=== Build finished: 1 errors, 0 warnings ===| ```

Original source

Related problems