问题描述
我对 C++ 还很陌生,所以请放轻松.
I'm pretty new to C++ so please go easy on me.
我正在尝试使用 sfml 创建一个 RenderWindow.然后,在创建播放器时,该播放器的关联窗口"对象被设置为之前创建的 RenderWindow.我的目的是能够从播放器对象运行窗口方法,例如 window.draw(),即:
I'm trying to use sfml to create a RenderWindow. Then, on creation of a player, that player's associated "window" object gets set to the RenderWindow created previously. My purpose is to be able to run window methods, such as window.draw(), from the player object, i.e.:
player::drawSprite() {
window.draw(sprite);
}
但是,我遇到了错误:
error: use of deleted function ‘sf::RenderWindow& sf::RenderWindow::operator=(const sf::RenderWindow&)’
window = win;
^
在错误日志的进一步下方,我还看到:
Further down in the error log, I also see:
error: initializing argument 1 of ‘Player::Player(sf::RenderWindow)’
Player(sf::RenderWindow win)
^
我的代码(省略了与问题无关的任何内容)如下:
My code (with anything not pertinent to the question omitted) is as follows:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <cstring>
#include <cstdlib>
#include <iostream>
class Player
{
private:
float x;
float y;
float speed;
sf::RenderWindow window;
public:
Player(sf::RenderWindow win)
{
x = 640;
y = 360;
speed = 5;
window = win;
}
};
int main()
{
//Window Initialization
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
//Player Creation
Player player(window);
}
我相信这个问题可能与 const 的、引用等有关,但我对 C++ 不够熟悉,无法轻松识别它.我该如何解决这个问题?
I believe the problem may have something to do with const's, references, or the like, but I am not familiar enough with C++ to easily identify it. How can I remedy this issue?
推荐答案
你应该使用引用或指向 window 对象的指针,因为我不认为你会希望每个玩家都有自己的窗口.
You should use reference or pointer to window object as I don't think that you will want each player to have its own window.
因此您的播放器应如下所示:
Thus your Player should look like:
class Player
{
private:
float x;
float y;
float speed;
sf::RenderWindow& window; // reference
public:
Player(sf::RenderWindow& win) // accepts reference
: window(win) // stores reference
{
x = 640;
y = 360;
speed = 5;
// window = win;
}
};
这篇关于使用删除函数‘sf::RenderWindow&sf::RenderWindow::operator=(const sf::RenderWindow&)’的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!