我是C ++的新手,来自Java。作为我的第一个项目,我想制作一个简单的SFML小游戏。我已经将主文件分为头文件和cpp文件。基本上,我在cpp类中初始化SFML RenderWindow。如您所见:
Main::Main() {
this->wWidth = 1440;
this->wHeight = 900;
this->wTitle = "Platformer";
this->fps = 0;
this->window = sf::RenderWindow(VideoMode(this->wWidth, this->wHeight), this->wTitle);
this->player = Player(100, 100, this->window);
}
我的头文件:
class Main {
public:
Main();
void tick();
void render();
void run();
void spawn();
int main();
float fps;
private:
int wWidth = 1440;
int wHeight = 900;
std::string wTitle = "Platformer";
sf::RenderWindow window;
Player player;
};
现在这可能是我来自Java的问题,但是我想在标头中创建sf::RenderWindow
的空实例,然后将其初始化为主文件。
但是将传递给sf::RenderWindow
初始化的Player
无效将不起作用。如果有人可以告诉我用C ++进行编码的正确方法,而不是用Java进行编码的方法,那会很好,因为我还没有完全理解C ++术语。
我得到的错误:
为此:
Main::Main() {
no matching function for call to 'Player::Player()'
为此:
this->window = sf::RenderWindow(VideoMode(this->wWidth, this->wHeight), this->wTitle);
use of deleted function 'sf::RenderWindow& sf::RenderWindow::operator=(const sf::RenderWindow&)'
为此:
this->player = Player(100, 100, this->window);
use of deleted function 'Player& Player::operator=(Player&&)'
为此
class Player {
non-static reference member 'sf::RenderWindow& Player::window', can't use default assignment operator
如果sf::RenderWindow
没有默认构造函数,则必须在Main
的初始化列表中对其进行初始化。这是因为您已声明window
为Main
的自动对象成员,因此在构造Main
时必须将其与Main
的所有其他成员一起构造。无论如何,您还应该使用初始化列表,因为它效率更高。
喜欢这个:
Main::Main() : fps(0), window(VideoMode(wWidth, wHeight), wTitle), player(100, 100, window) {}
(您不需要初始化所有其他成员,因为标题中的=
已将它们默认设置为初始化)