SFML community forums

Help => General => Topic started by: ecarusb on August 01, 2024, 07:38:07 pm

Title: [Solved]initial value of reference to non-const must be an lvalue
Post by: ecarusb on August 01, 2024, 07:38:07 pm
this might be more of a C++ question rather than SFML

why does the following code work
Collider playerCollider = player.GetCollider();

platform1.GetCollider().CheckCollision(playerCollider, 0.0f);
 

but this doesn't
platform1.GetCollider().CheckCollision(player.GetCollider(), 0.0f);
 

for reference this is player.h
class Player
{
public:
        Player(sf::Texture* texture, sf::Vector2u imageCount, float swtichTime, float speed);
        void Update(float deltaTime);
        void Draw(sf::RenderWindow& window);

        sf::Vector2f GetPosition() { return body.getPosition(); }
        Collider GetCollider() { return Collider(body); }
private:
        sf::RectangleShape body;
        Animations animation;
        unsigned int row;
        float speed;
        bool faceRight;
        float idleTime;
};

 

platform.h
class Platform
{
public:

        Platform(sf::Texture* texture, sf::Vector2f size, sf::Vector2f Postion);
        ~Platform();

        void Draw(sf::RenderWindow& window);
        Collider GetCollider() { return Collider(body); }
private :
        sf::RectangleShape body;
};

 

Collider.h
class Collider
{
public:
        Collider(sf::RectangleShape& body);
        ~Collider();

        void Move(float dx, float dy) { body.move(dx, dy);  }

        bool CheckCollision(Collider & other, float push);
        sf::Vector2f GetPosition() { return body.getPosition();}
        sf::Vector2f GetHalfSize() { return body.getSize() / 2.0f; }

       
private:
        sf::RectangleShape& body;
       
};

 
Title: Re: [Solved]Need help with a c++ question any help is appreciated
Post by: ecarusb on August 01, 2024, 08:38:39 pm
if anybody is wondering this explains the reason

https://stackoverflow.com/questions/1565600/how-come-a-non-const-reference-cannot-bind-to-a-temporary-object