SFML community forums

Help => Graphics => Topic started by: RetroRain on May 04, 2019, 07:21:06 pm

Title: I don't know why I am getting these errors trying to reference a sprite
Post by: RetroRain on May 04, 2019, 07:21:06 pm
The errors I get are:

In constructor 'Player::Player(sf::Sprite&)':
error: no match for call to '(sf::Sprite) ()'



Brief code with certain things that don't matter omitted (like the RenderWindow and other event stuff).


#include <SFML/Graphics.hpp>

using namespace sf;

class Player
{
  private:
        int bbox_top, bbox_bottom, bbox_left, bbox_right;    //collision for all sides of the player sprite
  public:
        Player(Sprite& playerSprite);   // or (const Sprite& playerSprite) not sure if the const is needed
        ~Player();
};

Player::Player(Sprite& playerSprite)
{
    bbox_top = playerSprite().getLocalBounds().y;
    bbox_bottom = playerSprite().getLocalBounds().height;
    bbox_left = playerSprite().getLocalBounds().x;
    bbox_right = playerSprite().getLocalBounds().width;
}

int main()
{
    Texture playerTexture;
    playerTexture.loadFromFile("player.png");
    Sprite playerSprite(playerTexture);

    Player player;
}
 


I'm referencing the sprite, so I don't get why I'm getting these errors.  It doesn't matter if I use const in the Player constructor parameter or not, I still get the error.  I even tried different names in the parameter list, like Sprite& ps.

I know this is more of a C++ question than SFML, but it is related to the sprites.  I don't get why it's not working.

If you know the answer, you're a life saver.  Thank you.
Title: Re: I don't know why I am getting these errors trying to reference a sprite
Post by: G. on May 04, 2019, 10:25:31 pm
playerSprite is a sprite, not a function and it doesn't have any () operator defined (like the error says)
So no playerSprite()
Title: Re: I don't know why I am getting these errors trying to reference a sprite
Post by: fallahn on May 04, 2019, 10:29:47 pm
Player player;
 

You're not referencing the sprite, it should be:

Player player(playerSprite);
 

ie, there's no default constructor for Player
Title: Re: I don't know why I am getting these errors trying to reference a sprite
Post by: RetroRain on May 05, 2019, 11:21:02 pm
G. and fallahn, thank you so much guys.  I really do appreciate it.  You guys are both life savers.  I understand both things you guys pointed out.

The first one, with the playerSprite(), I can't believe I didn't notice that.  I guess I got confused when I was using the getLocalBounds(), so I put the parentheses after the playerSprite as well.

And the other one, when creating the player object--this is the first time that I'm using parameters in the constructor, so I didn't know that I had to include those parameters when I create the player object.  But it makes sense to me.

I couldn't see my errors, even though they were clearly right in front of me.  I don't like posting a question on a forum unless it's a last resort, because I like to figure out what the problem is on my own.  But I just couldn't understand why I was getting those errors.

Thanks again guys. :)