我需要从存储在std :: list中的东西调用一个方法。目前,循环运行并且打印内部打印到控制台,但列表中的元素实际上并不打印它们应该的内容。以下是相关文件:
#include "TextWindow.hpp"
#include "TextElement.hpp"
#include <iostream>
TextWindow::TextWindow(const TextureHolder& textures, const FontHolder& fonts, sf::Window& windows, Fonts::ID fontID, sf::String text)
: mSprite(textures.get(Textures::WindowDefault))
, mHitpoints(10)
, mWindow(windows)
{
sf::FloatRect bounds = mSprite.getLocalBounds();
mSprite.setOrigin(bounds.width / 2.f, bounds.height / 2.f);
addElements(text, fonts.get(fontID));
}
void TextWindow::addElements(sf::String text, sf::Font font)
{
TextElement e(text, font, 10, xPos, yPos, 10, 10);
mElements.push_back(e);
}
void TextWindow::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(mSprite, states);
for(std::list<Element>::const_iterator iterator = mElements.begin(); iterator != mElements.end(); ++iterator)
{
//std::cout << "IN TEXTWINDOW PRINT ITERATOR" << std::endl;
(*iterator).drawCurrent(target, states);
}
}
void TextWindow::updateCurrent(sf::Time dt)
{
}
和
#include "TextElement.hpp"
#include <iostream>
TextElement::TextElement(sf::String text, sf::Font font, int textSize, int xOrig, int yOrig, int xPos, int yPos)
: mText(text, font, textSize)
, xpos(xPos)
, ypos(yPos)
{
mText.setOrigin(xOrig, yOrig);
mText.setColor(sf::Color::Green);
}
void TextElement::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
std::cout << "TEXTELEMENT" << std::endl;
target.draw(mText, states);
}
void TextElement::updateCurrent(sf::Time dt)
{
//Do nothing, text doesn't need to update!
}
有什么建议吗?
编辑:将列表更改为std :: unique_ptr后,出现以下错误:
1>------ Build started: Project: Byte, Configuration: Debug Win32 ------
1> TextWindow.cpp
1>e:\gamedev\c++\byte\byte\textwindow.cpp(19): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1> with
1> [
1> _Ty=Element
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(2350) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
1> with
1> [
1> _Ty=Element
1> ]
1> Generating Code...
1> Compiling...
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我已将对变量的所有引用更改为std :: unique_ptr。以下是新文件:
#include "TextWindow.hpp"
#include "TextElement.hpp"
#include <iostream>
TextWindow::TextWindow(const TextureHolder& textures, const FontHolder& fonts, sf::Window& windows, Fonts::ID fontID, sf::String text)
: mSprite(textures.get(Textures::WindowDefault))
, mHitpoints(10)
, mWindow(windows)
{
sf::FloatRect bounds = mSprite.getLocalBounds();
mSprite.setOrigin(bounds.width / 2.f, bounds.height / 2.f);
addElements(text, fonts.get(fontID));
}
void TextWindow::addElements(sf::String text, sf::Font font)
{
std::unique_ptr<TextElement> e(new TextElement(text, font, 10, xPos, yPos, 10, 10));
mElements.push_back(e);
}
void TextWindow::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(mSprite, states);
for(std::list<std::unique_ptr<Element>>::const_iterator iterator = mElements.begin(); iterator != mElements.end(); ++iterator)
{
//std::cout << "IN TEXTWINDOW PRINT ITERATOR" << std::endl;
(*iterator).get()->drawCurrent(target, states);
}
}
void TextWindow::updateCurrent(sf::Time dt)
{
}
答案 0 :(得分:3)
根据您在上面发布的内容,mElements
似乎是std::list<Element>
,我假设TextElement
继承自虚拟基类{{1 }}
这不会奏效。 Element
只存储mElements
个对象,而不是派生对象 - 这称为slicing problem。
您需要将Element
更改为mElements
(或std::list<unique_ptr<Element>>
),以便整个对象可以存储在列表中。