0
\$\begingroup\$

I'm currently learning to program a game with the SFML library.I dugged up a bunch of SFML tutorials on Youtube and found a really good one by 'Suraj Sharma':

https://www.youtube.com/channel/UC2i39AOpDSlO1Mrn1jQ8Xkg

I'm now making an animation class following his tutorial. Here's the video I'm watching:

https://www.youtube.com/watch?v=pq9OuBHJiVM&list=PL6xSOsbVA1ebkU66okpi-KViAO8_9DJKg&index=27

A C26495 warning:Variable 'AnimCmp::Timer' is uninitialized.Always initialize a member variable(type.6). Blockquote

and a

E0513 error:a value of type "AnimCmp::Anim*" cannot be assigned to an entity of type "AnimCmp::Anim*"

appears right when I was watching the first 8 minutes of the video. Here's my code, compiled in Visual Studio 2019 community (Take notes that all declared classes, functions and variables are an abbreviated version of the video):

AnimCmp.h:

#pragma once
#ifndef ANIM_CMP_H
#define ANIM_CMP_H
//i've put every required headers in a precompiled one
#include "pch.h"

class AnimCmp
{
private:
    class Anim {
    public:
        //Vars
        sf::Sprite& Spr;
        sf::Texture& TexSheet;
        sf::IntRect StartRct;
        sf::IntRect CurrRect;
        sf::IntRect EndRct;

        float Timer;
        float AnimTimer;
        int w;
        int h;
        //C26495 at Anim's constuctor
        Anim(sf::Sprite& Spr, sf::Texture& TexSheet, float AnimTimer, int stx, int sty, int frx, int fry, int w, int h)
            :Spr(Spr), TexSheet(TexSheet),AnimTimer(AnimTimer),w(w),h(h){
            this->w = w;
            this->h = h;
            this->StartRct = sf::IntRect(stx * w, sty * h, w, h);
            this->CurrRect = this->StartRct;
            this->EndRct = sf::IntRect(frx * w, fry * h, w, h);

            this->Spr.setTexture(this->TexSheet, true);
            this->Spr.setTextureRect(this->StartRct);
        }
        //virtual~Anim();
        //Functions
        //void Pause();
        void Reset() {
            this->Timer = 0.f;
            this->CurrRect = this->StartRct;
        }

        void Play(const float& dt) {
            //Update Timer
            this->Timer = 10.f*dt;
            if (this->Timer >= this->AnimTimer) {
                //Reset Timer
                this->Timer = 0.f;
                //Animate
                if (this->CurrRect != this->EndRct) {
                    this->CurrRect.left += this->w;
                }
                else {//Reset
                    this->CurrRect.left = this->StartRct.left;
                }
                this->Spr.setTextureRect(this->CurrRect);
            }
        }
    };

    sf::Sprite& Spr;
    sf::Texture& TexSheet;
    std::map<std::string, Anim*> Anims;
public:
    AnimCmp(sf::Sprite& Spr,sf::Texture& TexSheet);
    virtual~AnimCmp();
    //Functions
    void AddAnim(const std::string key, sf::Sprite& Spr, sf::Texture& TexSheet,
        float AnimTimer, int stx, int sty, int frx, int fry, int w, int h);

    void Start(const std::string Anim);
    void Pause(const std::string Anim);
    void Reset(const std::string Anim);
    void Play(const std::string key, const float& dt);
};
#endif // !ANIM_CMP_H

AnimCmp.cpp:

#include "AnimCmp.h"

AnimCmp::AnimCmp(sf::Sprite& Spr, sf::Texture& TexSheet)
    :Spr(Spr),TexSheet(TexSheet)
{
}

AnimCmp::~AnimCmp()
{
    for (auto& i : this->Anims) {
        delete i.second;
    }
}

void AnimCmp::AddAnim(const std::string key, sf::Sprite& Spr, sf::Texture& TexSheet,float AnimTimer, int stx, int sty,
 int frx, int fry, int w, int h)
{
    this->Anims[key] = new Anim(this->Spr, this->TexSheet, AnimTimer, stx, sty, frx, fry, w, h);//E0513
}
//Functions
void AnimCmp::Start(const std::string Anim)
{
}

void AnimCmp::Pause(const std::string Anim)
{
}

void AnimCmp::Reset(const std::string Anim)
{
}

void AnimCmp::Play(const std::string key, const float& dt)
{
    this->Anims[key]->Play(dt);
}

The thing is the game still runs even when an error is showing. I'm just not sure how it'll affect the game.

\$\endgroup\$
1
\$\begingroup\$

A C26495 warning:Variable 'AnimCmp::Timer' is uninitialized.Always initialize a member variable(type.6).

As it says, the compiler tells you that you have a class variable that is not initialized.

It will most likely look at the variable declaration

class AnimCmp
{
private:
    class Anim {
    public:
        //Vars
        float Timer;

...in the constructor initialization list...

            :Spr(Spr), TexSheet(TexSheet),AnimTimer(AnimTimer),w(w),h(h){

...or in the constructor body...

        {
            this->w = w;
            this->h = h;
            this->StartRct = sf::IntRect(stx * w, sty * h, w, h);
            this->CurrRect = this->StartRct;
            this->EndRct = sf::IntRect(frx * w, fry * h, w, h);

            this->Spr.setTexture(this->TexSheet, true);
            this->Spr.setTextureRect(this->StartRct);
        }

to find a place where you actually assign a value to the variable.

In no case here there is such a thing.

So you have the choice, either:

at the variable declaration

class AnimCmp
{
private:
    class Anim {
    public:
        //Vars
        float Timer {};

...in the constructor initialization list...

            :Spr(Spr), TexSheet(TexSheet),AnimTimer(AnimTimer),w(w),h(h),Timer(0.0f){

...or in the constructor body...

        {
            this->Timer = 0.0f;
            this->w = w;
            this->h = h;
            this->StartRct = sf::IntRect(stx * w, sty * h, w, h);
            this->CurrRect = this->StartRct;
            this->EndRct = sf::IntRect(frx * w, fry * h, w, h);

            this->Spr.setTexture(this->TexSheet, true);
            this->Spr.setTextureRect(this->StartRct);
        }

An uninitialized variable will always behave the same in debug mode because the compiler is happy to set a default value for you, but all hell can break loose in release. It's possible that your code is unaffected by such a thing, but the compiler does not know that. Better to initialize it for the sake of remove potential issues.


As for your other issue, I'm not exactly certain why it is occurring. Maybe you do not show everything that has to be shown for us to give you a definitive answer.

Shooting in the dark, and following a google search, you might have declared some parts of the map (or the function) as being const, or maybe the compiler thinks your c++ code is actually c code.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the help,so far the code still works fine and i haven't found anything wrong in debug mode.I'll take your suggestions and take a look. \$\endgroup\$ – TankRocket Oct 24 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.