从迭代器调用方法
Calling a method from an iterator
我需要从存储在 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:gamedevc++bytebytetextwindow.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.0vcincludememory(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)
{
}
从您上面发布的内容来看,看起来mElements
是一个std::list<Element>
,我假设TextElement
继承自虚拟基类Element
。
这是行不通的。 mElements
将只存储Element
对象,而不存储派生对象 - 这称为切片问题。
您需要将mElements
更改为std::list<unique_ptr<Element>>
(或shared_ptr
),以便整个对象可以存储在列表中。
相关文章:
- 如何强制从重写方法调用重写的方法基方法?
- C++:使用方法调用析构函数的顺序是什么?
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- 指向类方法调用的指针
- 如何使用 SFINAE 在方法调用中有条件地定义变量?
- 是否有可以处理方法调用依赖关系的设计模式?
- 如何缩短C++中的方法调用?
- 从部分专用模板方法调用模板非静态方法
- 有没有办法禁止派生类中的基类方法调用?
- 为什么这C++只在编译器上编码一个不明确的方法调用Microsoft?
- 从父方法调用子方法
- 如何将子方法调用到父方法
- 虚拟函数在哪里使用 vpointer to vtable 来解析方法调用,非虚拟方法存储在哪里以及如何解析它们?
- 从静态方法调用静态函数指针
- 从同一类中的另一个方法调用方法时出错
- 方法调用意外地像 l 值一样起作用
- 无法从派生的一个方法调用基类方法
- 从类方法调用命名空间中名为 Same 的函数时,重载解析失败
- C 多线程JAVA JNI方法调用