简体   繁体   English

玩家退出后向量超出范围

[英]Vector out of range, after logging player out

I'm having a major issue that I've tried to circumvent for many days now, but I can't come up with a good enough solution to fix it.我遇到了一个我已经尝试规避很多天的主要问题,但我无法想出一个足够好的解决方案来解决它。

The problem is in my client/server connection (specifically in the gameserver code), where when I log a player out and there are more than 1 players online, all players that has a higher index in the vector than the logged out player are now invalidated, thus crashing the server from the player vector being out of range.问题出在我的客户端/服务器连接中(特别是在游戏服务器代码中),当我注销一个玩家并且有超过 1 个在线玩家时,所有在向量中具有比注销玩家更高索引的玩家现在都是无效,从而使服务器从超出范围的玩家向量中崩溃。

I have tried to shorten the code, to only most relevant code for you to read easier.我试图缩短代码,只为最相关的代码让你更容易阅读。 I cannot include all the networking code, as it would be too much of a mess我不能包含所有的网络代码,因为它太混乱了

main.cpp主程序

#include <iostream>
#include <vector>

int main() {
        Player* newplayer = new Player(username, world);
        //Push two players into the vector, size is 1 after 0, 1
        world->players.push_back(newplayer);
        world->players.push_back(newplayer);

        //Log out player at index 0, then log player at index 1 out.
        for (int i = 0; i < world->characters.size(); i++) {
            world->Logout(world->players[0]);
            world->Logout(world->players[1]);
        }
    }
}

World.h/world.cpp World.h/world.cpp

//in world.h
std::vector<Player*> players;

//in world.cpp
void World::Logout(Player* player)
{
    world->Logout(world->players[0]);this->Logout(player->character);

    this->players.erase(
        std::remove(UTIL_RANGE(this->players), player),
        this->players.end()
    );
}

I don't know if I have explained myself properly, so please if you have any feedback as to how I can re-formulate my question or specify more details, please do let me know.我不知道我是否正确地解释了自己,所以如果您对我如何重新制定我的问题或指定更多细节有任何反馈,请告诉我。

Maybe, should I not use a vector at all?也许,我根本不应该使用向量? In this case, what do I need to use?在这种情况下,我需要使用什么? My program is reliant on that the players still have the same "player id" throughout the program.我的程序依赖于玩家在整个程序中仍然具有相同的“玩家 ID”。 If a player is removed, then that becomes a free element.如果一个玩家被移除,那么它就变成了一个自由元素。

Eg if there are 10 players in the vector.例如,如果向量中有 10 个玩家。 {player, player, player, player, player, player, player, player, player, player} and player at index 0 logs out, it should look like this {null/free, player, player, player, player, player, player, player, player, player} {播放器,播放器,播放器,播放器,播放器,播放器,播放器,播放器,播放器,播放器}和索引0处的播放器注销,它应该看起来像这样{空/免费,播放器,播放器,播放器,播放器,播放器,播放器,播放器,播放器,播放器}

Your code has a problem.你的代码有问题。 The docs say that std::remove will delete the element, and return a iterator.文档std::remove将删除元素,并返回一个迭代器。 You are then erasing the iterator with std::vector::erase .然后使用std::vector::erase迭代器。 However, the iterator returned from std::remove is past-the-end, thus causing a vector out-of-bounds error.但是,从std::remove返回的迭代器是越界的,从而导致向量越界错误。

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

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