C++ SFML 编译错误 sf::NonCopyable::NonCopyable(const sf::NonCopyable&) is private
C++ SFML Compile errors sf::NonCopyable::NonCopyable(const sf::NonCopyable&) is private
当我尝试编译以下代码时:
SFMLSet.cpp:
#include "SFMLSet.h"
SFMLSet::SFMLSet(string texturePath)
{
if(!texture.loadFromFile(texturePath)) {
exit(1);
}
new (&app) sf::RenderWindow(sf::VideoMode(texture.getSize().x, texture.getSize().y), texturePath, sf::Style::None);
new (&sprite) sf::Sprite(texture);
}
SFMLSet.h:
#ifndef SFMLSET_H
#define SFMLSET_H
#include <SFML/Graphics.hpp>
#include <string>
#include <cmath>
using namespace std;
class SFMLSet {
public:
sf::RenderWindow app;
sf::Texture texture;
sf::Sprite sprite;
sf::Vector2i grabbedOffset;
bool grabbedWindow = false;
SFMLSet (string texturePath);
sf::Event event;
};
#endif // SFMLSET_H
main.cpp:
#include <windows.h>
#include <vector>
#include <iostream>
#include "SFMLSet.h"
int main()
{
bool isRunning=true;
vector<SFMLSet> IMGS;
IMGS.push_back (SFMLSet ("cb.bmp"));
while (isRunning)
{
for (int i=0;i<IMGS.size();i++) {
while (IMGS[i].app.pollEvent(IMGS[i].event))
{
if (IMGS[i].event.type == sf::Event::Closed) {
IMGS[i].app.close();
isRunning=false;
}
if (IMGS[i].event.type == sf::Event::KeyPressed && IMGS[i].event.key.code == sf::Keyboard::Escape)
{
IMGS[i].app.close();
isRunning=false;
}
else if (IMGS[i].event.type == sf::Event::MouseButtonPressed)
{
if (IMGS[i].event.mouseButton.button == sf::Mouse::Left)
{
IMGS[i].grabbedOffset = IMGS[i].app.getPosition() - sf::Mouse::getPosition();
IMGS[i].grabbedWindow = true;
}
}
else if (IMGS[i].event.type == sf::Event::MouseButtonReleased)
{
if (IMGS[i].event.mouseButton.button == sf::Mouse::Left)
IMGS[i].grabbedWindow = false;
}
else if (IMGS[i].event.type == sf::Event::MouseMoved)
{
if (IMGS[i].grabbedWindow&&(IMGS[i].grabbedOffset.x<-10&&IMGS[i].grabbedOffset.y<-10)&&(IMGS[i].grabbedOffset.x>-(IMGS[i].texture.getSize().x)+10&&IMGS[i].grabbedOffset.y>-(IMGS[i].texture.getSize().y)+10))
IMGS[i].app.setPosition(sf::Mouse::getPosition() + IMGS[i].grabbedOffset);
}
}
IMGS[i].app.clear();
IMGS[i].app.draw(IMGS[i].sprite);
IMGS[i].app.display();
}
}
return EXIT_SUCCESS;
}
我遇到一些错误:
SFML-2.3.2\include/SFML/System/NonCopyable.hpp:67:5: error:
'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private
SFML-2.3.2\include/SFML/Window/Window.hpp:57:23: error: within this
context
SFML-2.3.2\include/SFML/System/NonCopyable.hpp:67:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private
SFML-2.3.2\include/SFML/Graphics/RenderTarget.hpp:51:25: error: within
this context
SFML-2.3.2\include/SFML/System/NonCopyable.hpp:79:18: error: 'sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCopyable&)'
is private
SFML-2.3.2\include/SFML/Window/Window.hpp:57:23: error:
within this context
SFML-2.3.2\include/SFML/System/NonCopyable.hpp:79:18: error: 'sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCopyable&)'
is private
SFML-2.3.2\include/SFML/Graphics/RenderTarget.hpp:51:25:
error: within this context
如何解决这个问题?
该错误消息告诉您您正试图在某处复制 sf::NonCopyable
的实例。编译器说您正在尝试调用那个 class 的复制构造函数,但该复制构造函数是私有定义的,因此无法访问。
要修复它,您需要找出导致 sc::NonCopyable
实例被复制的原因,并更改该代码,以便没有副本(可能使用指针)。
你应该阅读 SFML tutorials,并像他们的示例一样编写程序。
这里的具体问题是 sf::RenderWindow
的复制构造函数是私有的——通常复制 window.
是没有意义的
很遗憾,您在 std::vector
中使用了 SFMLSet
。向量必须动态地增加它们的大小,为了实现这一点,它们分配一个新的更大的缓冲区,并将它们现有的内容复制到新位置——调用 SFMLSet
的复制构造函数,后者又试图调用 sf::RenderWindow
s.
解决此问题的最佳方法可能是从 IMGS 中删除 sf::RenderWindow
,并将其作为局部变量保留在 main
中,再次如教程中所述。您可能不是要为每个图像打开一个新的 window,对吗?
当我尝试编译以下代码时:
SFMLSet.cpp:
#include "SFMLSet.h"
SFMLSet::SFMLSet(string texturePath)
{
if(!texture.loadFromFile(texturePath)) {
exit(1);
}
new (&app) sf::RenderWindow(sf::VideoMode(texture.getSize().x, texture.getSize().y), texturePath, sf::Style::None);
new (&sprite) sf::Sprite(texture);
}
SFMLSet.h:
#ifndef SFMLSET_H
#define SFMLSET_H
#include <SFML/Graphics.hpp>
#include <string>
#include <cmath>
using namespace std;
class SFMLSet {
public:
sf::RenderWindow app;
sf::Texture texture;
sf::Sprite sprite;
sf::Vector2i grabbedOffset;
bool grabbedWindow = false;
SFMLSet (string texturePath);
sf::Event event;
};
#endif // SFMLSET_H
main.cpp:
#include <windows.h>
#include <vector>
#include <iostream>
#include "SFMLSet.h"
int main()
{
bool isRunning=true;
vector<SFMLSet> IMGS;
IMGS.push_back (SFMLSet ("cb.bmp"));
while (isRunning)
{
for (int i=0;i<IMGS.size();i++) {
while (IMGS[i].app.pollEvent(IMGS[i].event))
{
if (IMGS[i].event.type == sf::Event::Closed) {
IMGS[i].app.close();
isRunning=false;
}
if (IMGS[i].event.type == sf::Event::KeyPressed && IMGS[i].event.key.code == sf::Keyboard::Escape)
{
IMGS[i].app.close();
isRunning=false;
}
else if (IMGS[i].event.type == sf::Event::MouseButtonPressed)
{
if (IMGS[i].event.mouseButton.button == sf::Mouse::Left)
{
IMGS[i].grabbedOffset = IMGS[i].app.getPosition() - sf::Mouse::getPosition();
IMGS[i].grabbedWindow = true;
}
}
else if (IMGS[i].event.type == sf::Event::MouseButtonReleased)
{
if (IMGS[i].event.mouseButton.button == sf::Mouse::Left)
IMGS[i].grabbedWindow = false;
}
else if (IMGS[i].event.type == sf::Event::MouseMoved)
{
if (IMGS[i].grabbedWindow&&(IMGS[i].grabbedOffset.x<-10&&IMGS[i].grabbedOffset.y<-10)&&(IMGS[i].grabbedOffset.x>-(IMGS[i].texture.getSize().x)+10&&IMGS[i].grabbedOffset.y>-(IMGS[i].texture.getSize().y)+10))
IMGS[i].app.setPosition(sf::Mouse::getPosition() + IMGS[i].grabbedOffset);
}
}
IMGS[i].app.clear();
IMGS[i].app.draw(IMGS[i].sprite);
IMGS[i].app.display();
}
}
return EXIT_SUCCESS;
}
我遇到一些错误:
SFML-2.3.2\include/SFML/System/NonCopyable.hpp:67:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private
SFML-2.3.2\include/SFML/Window/Window.hpp:57:23: error: within this context
SFML-2.3.2\include/SFML/System/NonCopyable.hpp:67:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private
SFML-2.3.2\include/SFML/Graphics/RenderTarget.hpp:51:25: error: within this context
SFML-2.3.2\include/SFML/System/NonCopyable.hpp:79:18: error: 'sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCopyable&)' is private
SFML-2.3.2\include/SFML/Window/Window.hpp:57:23: error: within this context
SFML-2.3.2\include/SFML/System/NonCopyable.hpp:79:18: error: 'sf::NonCopyable& sf::NonCopyable::operator=(const sf::NonCopyable&)' is private
SFML-2.3.2\include/SFML/Graphics/RenderTarget.hpp:51:25: error: within this context
如何解决这个问题?
该错误消息告诉您您正试图在某处复制 sf::NonCopyable
的实例。编译器说您正在尝试调用那个 class 的复制构造函数,但该复制构造函数是私有定义的,因此无法访问。
要修复它,您需要找出导致 sc::NonCopyable
实例被复制的原因,并更改该代码,以便没有副本(可能使用指针)。
你应该阅读 SFML tutorials,并像他们的示例一样编写程序。
这里的具体问题是 sf::RenderWindow
的复制构造函数是私有的——通常复制 window.
很遗憾,您在 std::vector
中使用了 SFMLSet
。向量必须动态地增加它们的大小,为了实现这一点,它们分配一个新的更大的缓冲区,并将它们现有的内容复制到新位置——调用 SFMLSet
的复制构造函数,后者又试图调用 sf::RenderWindow
s.
解决此问题的最佳方法可能是从 IMGS 中删除 sf::RenderWindow
,并将其作为局部变量保留在 main
中,再次如教程中所述。您可能不是要为每个图像打开一个新的 window,对吗?