二进制'==':找不到运算符-TCP套接字SFML

时间:2018-11-27 10:31:15

标签: c++ networking sfml

我正在尝试遍历std::list<sf::TcpSocket> clients,并从sf::SocketSelector和列表本身中删除断开连接的那些。 当尝试使用迭代器从列表中删除客户端时,我不断收到“二进制'=='找不到运算符”错误。 这是从以下位置触发错误的代码部分:

std::list<sf::TcpSocket> clients;
std::list<sf::TcpSocket>::iterator i;

for (auto i = clients.begin(); i != clients.end();)
{
    if (selector.isReady(*i))
    {
        sf::Socket::Status status = i->receive(dummy, 1, received);
        if (status != sf::Socket::Done)
        {
            if (status == sf::Socket::Disconnected)
            {
                selector.remove(*i);
                clients.remove(*i);  // this causes the error
            }
        }
        else
        {
            //i++;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用其迭代器删除对象,您已经拥有它:

std::list<sf::TcpSocket> clients;
std::list<sf::TcpSocket>::iterator i;

for (auto i = clients.begin(); i != clients.end();)
{
    if (selector.isReady(*i))
    {
        sf::Socket::Status status = i->receive(dummy, 1, received);
        if (status != sf::Socket::Done)
        {
            if (status == sf::Socket::Disconnected)
            {
                selector.remove(*i);
                i = clients.erase(i); // Properly update the iterator
            }
        }
        else
        {
            ++i;
        }
    }
}