简体   繁体   English

SFML和STL:我的代码有什么问题?

[英]SFML and STL: What's wrong with my code?

I have the following code snippets: 我有以下代码片段:

void RemoveButton::triggerAction(team &team, unsigned int index)
{
    switch (mAction)
    {
        case Action::remove:

        {
            //team.mTeamMembers.erase(std::remove(team.mTeamMembers.begin(), team.mTeamMembers.end(), team.mTeamMembers.at(index)), team.mTeamMembers.end());
            team.mTeamMembers.erase(team.mTeamMembers.begin() + index);

            for (unsigned int i = index; i < team.mTeamMembers.size(); i++)
            {
                team.mTeamMembers[i].mRemoveButton->getText().move(0.0f, -30.0f);
                team.mTeamMembers[i].mText.move(0.0f, -30.0f);
            }

            team.mAddPosition.y -= 30.0f;
            break;
        }

        default:break;
    }
}

class team
{
public:
    size_t                                        teamNumber;
    std::vector<AddButton>                        mAddButtons;
    std::vector<teamRecord>                        mTeamMembers;

    sf::Sprite                                    mBorder;
    sf::Text                                    mText;
    Selector<AddButton>                            mAddButtons_Selector;
    sf::Vector2f                                mAddPosition;
    Selector<teamRecord>                        mTeamMembers_Selector;

    team(sf::Vector2f borderPosition, sf::Vector2f removeButtonsPosition, sf::Vector2f textPosition, size_t teamNumb, std::string text);
};

How should I erase an element from the team.mTeamMembers STL vector? 应该如何删除team.mTeamMembers STL向量中的元素? The way I do it currently results in seemingly random execution errors, specifically when clicking the non-existent sprite of a deleted button. 目前,我执行此操作的方式会导致看似随机的执行错误,尤其是在单击已删除按钮的不存在的精灵时。 The commented line that uses std::remove is not compiled, and I do not understand why. 使用std :: remove的注释行未编译,我不明白为什么。 As I'm feeling confused af , can someone please shine some light here? 当我感到困惑后,有人可以在这里发光吗? Here's the build log while using std::remove, as recommended by some users on this forum: 这是一些用户在此论坛上建议的使用std :: remove时的构建日志:

1>------ Build started: Project: Complex OOP Menus, Configuration: Release Win32 ------
1>  RemoveButton.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\algorithm(1454): error C2678: binary '==': no operator found which takes a left-hand operand of type 'teamRecord' (or there is no acceptable conversion)
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(389): note: could be 'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(381): note: or       'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(373): note: or       'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\system_error(365): note: or       'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\exception(339): note: or       'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) throw()'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\exception(334): note: or       'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) throw()'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\exception(329): note: or       'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) throw()'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\algorithm(1454): note: while trying to match the argument list '(teamRecord, const teamRecord)'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\algorithm(1467): note: see reference to function template instantiation '_FwdIt std::_Remove_unchecked<teamRecord*,_Ty>(_FwdIt,_FwdIt,const _Ty &)' being compiled
1>          with
1>          [
1>              _FwdIt=teamRecord *,
1>              _Ty=teamRecord
1>          ]
1>  ..\Data\Source\RemoveButton.cpp(24): note: see reference to function template instantiation '_FwdIt std::remove<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<teamRecord>>>,teamRecord>(_FwdIt,_FwdIt,const _Ty &)' being compiled
1>          with
1>          [
1>              _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<teamRecord>>>,
1>              _Ty=teamRecord
1>          ]

If there is not enough information here, I could post the github link to my project, but it's pretty big and it would take time to sift through the code. 如果这里没有足够的信息,我可以将github链接发布到我的项目中,但是它很大,并且需要花一些时间来浏览代码。

Using std::remove() doesn't make any sense to me if you already know the position/offset and you only want to remove a single entry. 如果您已经知道位置/偏移量,并且只想删除一个条目,则使用std::remove()对我来说没有任何意义。

You're receiving your error with std::remove() most likely due to the fact that there's no equality operator ( operator == ) defined for your teamRecord class (or it just isn't found). 您收到std::remove()错误的原因很可能是因为没有为teamRecord类定义相等运算符( operator == )(或者找不到)。

The way I do it currently results in seemingly random execution errors, specifically when clicking the non-existent sprite of a deleted button. 目前,我执行此操作的方式会导致看似随机的执行错误,尤其是在单击已删除按钮的不存在的精灵时。

That's something we can't look at unless we see the code regarding that. 除非我们看到与此相关的代码,否则我们将无法看清这一点。 How can you click something not existing? 您如何单击不存在的内容? Any chance you're just missing a check or not cleaning up things properly? 您是否只是错过了支票或没有正确清理东西?

I solved this by double checking the index I was giving to the elements on insertion. 我通过仔细检查插入时赋予元素的索引来解决此问题。 Thanks and sorry for such a stupid mistake! 谢谢,抱歉犯了这么愚蠢的错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM