gpt4 book ai didi

C++ SFML 错误 sf::NonCopyable::Noncopyable

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

我找不到解决代码中此错误的方法,在谷歌搜索并检查源代码后,我找不到解决它的方法。

错误:

Error   1   error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'  d:\sfml-2.3.1\include\sfml\window\window.hpp    521

 Error  2   error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'  d:\sfml-2.3.1\include\sfml\graphics\rendertarget.hpp    419

这是我认为错误所在的代码,否则就是很多代码。如果您需要完整代码,请提出要求,我会快速编辑这篇文章。

场景节点.h

#ifndef SCENENODE_H
#define SCENENODE_H

#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Transformable.hpp>
#include <SFML/System/NonCopyable.hpp>
#include <SFML/System/Time.hpp>

#include <vector>
#include <memory>

class SceneNode : public sf::Transformable, public sf::Drawable, private sf::NonCopyable
{
public:
typedef std::unique_ptr<SceneNode> Ptr;

private:
std::vector<Ptr> children;
SceneNode* parent;

virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;

void drawChildren(sf::RenderTarget& target, sf::RenderStates states) const;
virtual void updateCurrent(sf::Time dt);
void updateChildren(sf::Time dt);

public:
SceneNode();

void update(sf::Time dt);
void attachChild(Ptr child);
Ptr detachChild(const SceneNode& node);

sf::Transform getWorldTransform() const;
sf::Vector2f getWorldPosition() const;
};

#endif

场景节点.cpp

#include "SceneNode.h"
#include <cassert>

SceneNode::SceneNode() : parent(nullptr) {}

void SceneNode::attachChild(Ptr child){
child->parent = this;
children.push_back(std::move(child));
}

SceneNode::Ptr SceneNode::detachChild(const SceneNode& node){
auto found = std::find_if(children.begin(), children.end(),
[&](Ptr& p) {return p.get() == &node; });

assert(found != children.end());

Ptr result = std::move(*found);
result->parent = nullptr;
children.erase(found);
return result;
}

void SceneNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const{}

void SceneNode::draw(sf::RenderTarget& target, sf::RenderStates states) const{
states.transform *= getTransform();

drawCurrent(target, states);
drawChildren(target, states);
}

void SceneNode::update(sf::Time dt){
updateChildren(dt);
updateCurrent(dt);
}

void SceneNode::updateChildren(sf::Time dt){
for (Ptr& child : children)
child->update(dt);
}

void SceneNode::updateCurrent(sf::Time dt){
for (Ptr& child : children)
child->update(dt);
}

void SceneNode::drawChildren(sf::RenderTarget& target, sf::RenderStates states) const{
for(const Ptr& child : children)
child->draw(target, states);
}

sf::Transform SceneNode::getWorldTransform() const{
sf::Transform transform = sf::Transform::Identity;

for (const SceneNode* node = this; node != nullptr; node = node->parent)
transform = node->getTransform() * transform;

return transform;
}

sf::Vector2f SceneNode::getWorldPosition() const{
return getWorldTransform() * sf::Vector2f();
}

飞机.h

#ifndef AIRPLANE_H
#define AIRPLANE_H

#include "Entity.h"
#include "ResourceIdentifiers.h"

#include <SFML/Graphics/Sprite.hpp>

class Airplane : public Entity
{
public:
enum Type{
Raptor, Eagle,
};

Airplane(Type type, const TextureHolder& textures);

private:
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;

Type type;
sf::Sprite sprite;
};

#endif

飞机.cpp

#include "Airplane.h"

#include "ResourceHolder.h"

Textures::ID toTextureID(Airplane::Type type){
switch (type){
case Airplane::Eagle:
return Textures::Eagle;
case Airplane::Raptor:
return Textures::Raptor;
}
return Textures::Eagle;
}

Airplane::Airplane(Type _type, const TextureHolder& textures)
: type(_type), sprite(textures.get(toTextureID(type)))
{
sf::FloatRect bounds = sprite.getLocalBounds();
sprite.setOrigin(bounds.width / 2.f, bounds.height / 2.f);
}

void Airplane::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const{
target.draw(sprite, states);
}

Sprite 节点.h

#ifndef SPRITENODE_H
#define SPRITENODE_H

#include "SceneNode.h"
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/RenderStates.hpp>
#include <SFML/Graphics/RenderTarget.hpp>

class SpriteNode : public SceneNode
{
sf::Sprite sprite;

virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
public:
explicit SpriteNode(const sf::Texture& texture);
SpriteNode(const sf::Texture& texture, const sf::IntRect& rect);
};

#endif

Sprite 节点.cpp

#include "SpriteNode.h"

SpriteNode::SpriteNode(const sf::Texture& texture) : sprite(texture)
{ }

SpriteNode::SpriteNode(const sf::Texture& texture, const sf::IntRect& rect) : sprite(texture, rect)
{ }

void SpriteNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const{
target.draw(sprite, states);
}

世界.h

#ifndef WORLD_H
#define WORLD_H

#include <SFML/System/NonCopyable.hpp>
#include <SFML/Graphics/View.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <array>
#include "ResourceIdentifiers.h"
#include "ResourceHolder.h"
#include "SceneNode.h"
#include "SpriteNode.h"
#include "Airplane.h"

class World : private sf::NonCopyable
{
enum Layer{
Background,
Air,
LayerCount
};

sf::RenderWindow window;
sf::View worldView;
TextureHolder textures;
SceneNode sceneGraph;
std::array<SceneNode*, LayerCount> sceneLayers;

sf::FloatRect worldBounds;
sf::Vector2f spawnPosition;
float scrollSpeed;
Airplane* playerAirplane;

void loadTextures();
void buildScene();
public:
explicit World(sf::RenderWindow& window);
void update(sf::Time dt);
void draw();
};

#endif

世界.cpp

#include "World.h"

World::World(sf::RenderWindow& win) : window(win),
worldView(win.getDefaultView()),
worldBounds(0.f, 0.f, worldView.getSize().x, 2000.f),
spawnPosition(worldView.getSize().x / 2.f, worldBounds.height - worldView.getSize().y / 2.f),
scrollSpeed(-50.f),
playerAirplane(nullptr),
textures()
{
loadTextures();
buildScene();

worldView.setCenter(spawnPosition);
}

void World::loadTextures(){
textures.load(Textures::Eagle, "D:/SFMLpro/Eagle.png");
textures.load(Textures::Raptor, "D:/SFMLpro/Raptor.png");
textures.load(Textures::Desert, "D:/SFMLpro/Desert.png");
}

void World::buildScene(){
for (std::size_t i = 0; i < LayerCount; ++i){
SceneNode::Ptr layer(new SceneNode());
sceneLayers[i] = layer.get();

sceneGraph.attachChild(std::move(layer));
}

sf::Texture& texture = textures.get(Textures::Desert);
sf::IntRect textureRect(worldBounds);
texture.setRepeated(true);

std::unique_ptr<SpriteNode> backgroundSprite(new SpriteNode(texture, textureRect));
backgroundSprite->setPosition(worldBounds.left, worldBounds.top);
sceneLayers[Background]->attachChild(std::move(backgroundSprite));

std::unique_ptr<Airplane> leader(new Airplane(Airplane::Eagle, textures));
playerAirplane = leader.get();
playerAirplane->setPosition(spawnPosition);
playerAirplane->setVelocity(40.f, scrollSpeed);
sceneLayers[Air]->attachChild(std::move(leader));

std::unique_ptr<Airplane> leftEscort(new Airplane(Airplane::Raptor, textures));
leftEscort->setPosition(-80.f, 50.f);
playerAirplane->attachChild(std::move(leftEscort));

std::unique_ptr<Airplane> rightEscort(new Airplane(Airplane::Raptor, textures));
rightEscort->setPosition(80.f, 50.f);
playerAirplane->attachChild(std::move(leftEscort));
}

void World::draw(){
window.setView(worldView);
window.draw(sceneGraph);
}

void World::update(sf::Time dt){
worldView.move(0.f, scrollSpeed * dt.asSeconds());

sf::Vector2f position = playerAirplane->getPosition();
sf::Vector2f velocity = playerAirplane->getVelocity();

if (position.x <= worldBounds.left + 150 || position.x >= worldBounds.left + worldBounds.width - 150){
velocity.x = -velocity.x;
playerAirplane->setVelocity(velocity);
}

sceneGraph.update(dt);
}

游戏.h

#ifndef GAME_H
#define GAME_H

#include "ResourceHolder.h"
#include "World.h"
#include <SFML/Graphics/RenderStates.hpp>
#include <SFML/Graphics/RenderTarget.hpp>

class Game{
sf::RenderWindow window;
World world;

void processEvents();
void update(sf::Time deltaTime);
void render();

void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);

public:
Game();
void run();
};

#endif

游戏.cpp

#include "Game.h"

Game::Game() : window(sf::VideoMode(1600, 900), "SFML"), world(window) {}

void Game::run(){
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
const sf::Time timePerFrame = sf::seconds(1.f / 60.f);

while (window.isOpen()){
processEvents();
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > timePerFrame){
timeSinceLastUpdate -= timePerFrame;
processEvents();
update(timePerFrame);
}
render();
}
}

void Game::processEvents(){
sf::Event evt;
while (window.pollEvent(evt)){
switch (evt.type){
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
handlePlayerInput(evt.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(evt.key.code, false);
}
}
}

void Game::update(sf::Time deltaTime){
world.update(deltaTime);
}

void Game::render(){
window.clear();
world.draw();

window.setView(window.getDefaultView());
window.display();
}

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed){}

谢谢

最佳答案

很多代码,但是在这里

World::World(sf::RenderWindow& win) : window(win)

您想复制 sf::RenderWindow,但它似乎通过从 NonCopyable 派生来禁用复制构造函数。

编辑(回应评论):如果你的意思是他问如何修复,如果他确定 sf::RenderWindow 的实例将在整个生命周期内保持有效,则该成员可以作为引用World 对象的...

关于C++ SFML 错误 sf::NonCopyable::Noncopyable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768203/

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