从迭代器调用方法

Calling a method from an iterator

本文关键字:方法 调用 迭代器      更新时间:2023-10-16

我需要从存储在 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),以便整个对象可以存储在列表中。