参数类型与指针/引用不匹配
parameter type mismatch with pointer/reference
我正在尝试将玩家添加到我正在创建的游戏中,但在此过程中,当我在mainGame.cpp中创建新玩家时,我一直在窗口参数上获得错误
这个问题是一个指针/引用问题,但我不知道如何解决它。
这是错误信息:
参数类型不匹配:不兼容类型'sf::RenderWindow &'和'sf::RenderWindow *'
my 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;
}
my mainGame.h file:
class mainGame : public tiny_state{
public:
void Initialize(sf::RenderWindow* window);
void Destroy(sf::RenderWindow* window);
protected:
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),
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);
}
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;
};
你传递了一个指针,在这里需要引用。废弃:
this->player = new Player(20,100, config, *window);
^
顺便说一下,考虑使用智能指针,比如unique_ptr
来管理内存。这样你就可以使用0/3/5规则,而不是打破3/5规则的一部分。
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- 关于类的 Python 文档 - 对C++的引用不正确
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 引用不完整的类类型 - C++
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 类型为 "Bucket&"(未限定的 const 限定)的引用不能使用 "SortedList." 类型的值进行初始化 如何修复此错误?
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 带有反向引用的正则表达式在C++中不匹配
- 参数类型与指针/引用不匹配