Home > Net >  a pointer to a bound function may only be used to call the function using a setter
a pointer to a bound function may only be used to call the function using a setter

Time:12-05

I am using an SDL Template and I am trying to implement gravity into my code so I am trying to set a Y as the strength of my gravity in update but when I try to use a pointer to setY I always get an error "a pointer to a bound function may only be used to call the function using a setter" the code below is from my GameScene.cpp

#include "GameScene.h"

    GameScene::GameScene()
    {
        // Register and add game objects on constructor
        player = new Player();
        this->addGameObject(player);

        floor = new Floor();
        this->addGameObject(floor);
    }

    GameScene::~GameScene()
    {
        delete player;
    }

    void GameScene::start()
    {
        Scene::start();


        // Initialize any scene logic here
    }

    void GameScene::draw()
    {
        Scene::draw();
    }

    void GameScene::update()
    {
        Scene::update();

        if (player->getOnFloor() == false) {
            player->setY -= 1;
        }

        else {
            player->setY = 0;
        }
    }

This code is from my Player.h where the setters and getters are located

    #pragma once
    #include "GameObject.h"
    #include "common.h"
    #include "draw.h"

    class Player :
        public GameObject
    {
    public:
        void start();
        void update();
        void draw();

        //X Setter
        void setX(int x) {
            x = x;
        }
        //X Getter
        int getX() {
            return x;
        }
        //Y Setter
        void setY(int y) {
            y = y;
        }
        //Y Getter
        int getY() {
            return y;
        }
        //Height Setter
        void setHeight(int height) {
            height = height;
        }
        //Height Getter
        int getHeight() {
            return height;
        }
        //Width Setter
        void setWidth(int width) {
            width = width;
        }
        //Width Getter
        int getWidth() {
            return width;
        }
        //OnFloor Setter
        void setOnFloor(int onFloor) {
            onFloor = onFloor;
        }
        //OnFloor Getter
        int getOnFloor() {
            return onFloor;
        }
    private:
        SDL_Texture* texture;
        int x;
        int y;
        int height;
        int width;
        int speed;
        bool onFloor;

    };

I have tried putting () beside the setY like this

    if (player->getOnFloor() == false) {
        player->setY() -= 1;
    }

    else {
        player->setY)( = 0;
    }

But it still did not work, I was expecting it to make my player fall down but VS studio kept showing me the error

this may be a noobie question but I am still a noobie programmer so please bear with me

EDITED:

The player is now falling upwards, I tried doing -= 1 to make him fall downwards

void GameScene::update()
{
    Scene::update();

    if (player->getOnFloor() == false) {
        player->setY(player->getY() -= 1);
    }

    else {
        player->setY(0);
    }
}

but I get an error on player in "player->getY()" saying that expression must be a modifiable lvalue

CodePudding user response:

The error you are getting is because you are trying to use the setY setter function as a variable. In order to use the setter function to set the y property of the player object, you need to call the setY function and pass in the new value for y as an argument. Here is how you would use the setY setter function in your GameScene::update function:

void GameScene::update()
{
    Scene::update();

    if (player->getOnFloor() == false) {
        player->setY(player->getY()   1);
    }

    else {
        player->setY(0);
    }
}

Note that in this example, we are calling the getY getter function to get the current value of y, and then subtracting 1 from it before passing the result to the setY setter function. This will update the y property of the player object and make it fall down by 1 unit each time the update function is called.

  • Related