gpt4 book ai didi

c++ - 参数类型与指针/引用不匹配

转载 作者:行者123 更新时间:2023-11-27 22:48:46 26 4
gpt4 key购买 nike

我正在尝试将玩家添加到我创建的游戏中,但在此过程中,当我在 mainGame.cpp 中创建新玩家时,窗口参数一直出现错误

问题是指针/引用问题,但我不知道如何解决。

这是错误信息:

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

我的 mainGame.cpp 看起来像这样:

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

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

我的 mainGame.h 文件:

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

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

我的 Plyer.cpp 文件:

Player::Player(float x, float y, const Config& config, sf::RenderWindow& )
: x(x), y(y),
config(config),
window(window)
{
rectangle.setSize(sf::Vector2f(sizeWidth, sizeHeight));
rectangle.setFillColor(sf::Color::White);
}

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);
window.draw(rectangle);
}

我的 player.h 文件:

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;
};

最佳答案

您正在将指针传递到需要引用的位置。取消引用它:

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

顺便说一下,考虑使用智能指针,比如 unique_ptr管理你的内存。这样你就可以雇用rule of zero/three/five而不是打破其中三/五部分的规则。

关于c++ - 参数类型与指针/引用不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40005555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com