Search code examples
c++objectpointerssfml

Pointer to an object


I have a problem. I am developing a small chess-like game. I am creating a class that manages the round, dividing them into 3 phases. In the first step, check if the mouse has clicked on a Token. In the second check select a location And in the third he moves the selected Token.

I had thought in the first phase (after clicking on a Token) to make the token point to a pointer, and to continue phases two and three with this pointer (so as to save code when adding more tokens). But I am having problems, the program does not give errors, but when I get to the third phase it crashes.

TurnSystem.h

enum PHASE {pawnSelection=0, positionSelection=1, motionAnimation=2};

class TurnSystem{
private:
    short phase = PHASE::pawnSelection;

    //TOKEN
    Token* soldier;
    Token* demon;
    Token* A;

    void InizializedToken();

public:
    TurnSystem();
    virtual ~TurnSystem();

    void Update(sf::Vector2i& mousePos);
};

TurnSystem.cpp

TurnSystem::TurnSystem() {
    this->InizializedToken();
}
TurnSystem::~TurnSystem() {

}

void TurnSystem::InizializedToken() {
    sf::Image image;
    image.loadFromFile("C:/Progect/Sprites/Soldier.png");
    this->soldier= new Token(image,false,10,10,5,1,1);
    image.loadFromFile("C:/Progect/Dimon.png");
    this->demon= new Token(image,true,6,7,6,4,4);
}

void TurnSystem::Update(sf::Vector2i &mousePos) {
    if (this->phase == PHASE::pawnSelection){
        if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
            if(soldier->sprite.getGlobalBounds().contains(mousePos.x,mousePos.y)){
                Token* A = soldier;
                phase=PHASE::positionSelection;
            }
            else if (demon->sprite.getGlobalBounds().contains(mousePos.x,mousePos.y)){
                std::cout << "Enemy token, select another one \n";
            }
        }
    }
    else if (this->phase == PHASE::positionSelection){
        //doSomthing
        phase=PHASE::motionAnimation;
    }
    else if(this->phase == PHASE::motionAnimation){
        A->move(); //the program stops here
    }
}

The Token class has a method called Move. But I don't know how to use it via the pointer

Thanks in advance and sorry for any grammatical errors, I'm not native speakers


Solution

  • You've redeclared your A pointer hiding the version you meant to assign to. See the comments

    class TurnSystem{
        ...
        Token* A; // class member A
        ...
    };
    
    void TurnSystem::Update(sf::Vector2i &mousePos) {
        if (...){
            if (...) {
                if(...){
                    Token* A = soldier; // this is a local variable called A
                    phase=PHASE::positionSelection;
                }
                ...
            }
        }
        ...
        else if(...){
            A->move(); // this is the class member called A
        }
    

    The fix is to remove the declalration.

                    A = soldier; // now this is the class member called A