ebk ebk - 9 days ago 6x
C++ Question

parameter type mismatch with pointer/reference

I´m trying to add a player to a game I´m creating, but during this process I keep getting an error on the window parameter when I´m creating a new player in mainGame.cpp

The issue is a pointer/reference issue, but I can´t figure out how to fix it.

This is the error message:

Parameter type mismatch: Incompatible types 'sf::RenderWindow &' and 'sf::RenderWindow *'

my mainGame.cpp look like this:

void mainGame::Initialize(sf::RenderWindow* window){
this->player = new Player(20,100, config, window);

void mainGame::Destroy(sf::RenderWindow* window){
delete this->player;

my mainGame.h file:

class mainGame : public tiny_state{
void Initialize(sf::RenderWindow* window);
void Destroy(sf::RenderWindow* window);

Player& player;
Config config;
sf::RenderWindow window;

my Plyer.cpp file:

Player::Player(float x, float y, const Config& config, sf::RenderWindow& )
: x(x), y(y),
rectangle.setSize(sf::Vector2f(sizeWidth, sizeHeight));

void Player::move(float delta){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
y -= speed * delta;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
y += speed * delta;

y = std::max(y, 0.f);
y = std::min(y, (float)(config.screenheight - sizeHeight));

void Player::draw(){
rectangle.setPosition(x, y);

my player.h file:

struct Player{
Player(float x, float y, const Config& config, sf::RenderWindow& window);

void move(float delta);
void draw();

const int sizeHeight = 100;
const int sizeWidth = 10;
const float speed = 5;
float x, y;

sf::RectangleShape rectangle;
const Config& config;
sf::RenderWindow& window;


You're passing a pointer where a reference is expected. Dereference it:

this->player = new Player(20,100, config, *window);

By the way, consider using smart pointers, like unique_ptr to manage your memory. This way you'll be able to employ rule of zero/three/five instead of breaking of rule of three/five part of it.