【发布时间】:2019-07-25 01:31:48
【问题描述】:
我正在尝试在 SFML 中创建一个矩形矩阵,并让它们看起来像地板。为此,我想使用唯一指针向量。不过好像有一个我不太明白的错误。
在成功初始化向量后,它在同一个函数中被声明为删除。当我做类似的实现但使用new 或共享指针时,没有问题。
是什么导致了这个错误,我该如何解决?发生地点如下图:
代码:
sf::Texture texture;
texture.loadFromFile("./resources/wood.jpg");
std::vector<std::unique_ptr<sf::Sprite>> floor;
unsigned counter = 0;
float posX = 0.f, posY = 0.f;
for (int i = 0; i < 50; i++) {
floor.push_back(std::make_unique<sf::Sprite>());
floor[i]->setTexture(texture);
floor[i]->setTextureRect(sf::IntRect(1, 1, 100, 100));
floor[i]->setPosition(sf::Vector2f(posX, posY));
counter++;
posX += 100.f;
if (counter == 10) {
posY += 100.f;
posX = 0.f;
counter = 0;
}
}
while (window.isOpen()) {
sf::Event eH;
for (auto &sprite : floor)
window.draw(*sprite.get());
while (window.pollEvent(eH)) {
if (eH.type == sf::Event::Closed)
window.close();
if (eH.type == sf::Event::KeyReleased && eH.key.code == sf::Keyboard::Escape)
window.close();
if (eH.type == sf::Event::Resized)
glViewport(0, 0, eH.size.width, eH.size.height);
}
window.display();
window.clear(sf::Color::Black);
错误描述:
'std::unique_ptr<:sprite>>::unique_ptr(const std::unique_ptr<_ty>> &)': 试图引用一个删除功能
【问题讨论】:
-
请勿发布“格式化代码的 PNG”,请改为发布您的代码。
-
添加代码,不是代码截图。
-
并复制粘贴 full 和 complete 错误输出(formatted 作为代码)。
-
请花时间阅读tour 并阅读How to Ask,然后阅读edit 相应的问题。请创建a Minimal, Complete, and Verifiable example,并提供示例输入、输出和您收到的错误消息(如果有)。这将帮助我们确定正在发生的事情并提高您获得答案的机会。
-
好的,当我再次要求时,我会记住这一点。
标签: c++ sfml unique-ptr