0

我正在编写 IRC 服务器,并且遇到了operator<<.

我有IrcServerIrcClient类,我希望能够输出一个服务器和一个客户端。

我想要的是获得这种输出:

 nick |  user    |    host  |   server   |   name

使用我自己的客户端连接到服务器,它工作正常,我得到这个输出:

NICKNAME->mynick | USERNAME->myuser | HOSTNAME->myhost | SERVERNAME->127.0.0.1 | REALNAME->my name

但是使用 irssi,它不起作用,我只得到这个输出(缺少 NICKNAME):

| USERNAME->myuser | HOSTNAME->myhost | SERVERNAME->127.0.0.1 | REALNAME->my name

这是我使用的代码:

ostream_irc_client.hpp:

#ifndef OSTREAM_IRC_CLIENT_HPP
# define OSTREAM_IRC_CLIENT_HPP

# include <iostream>

# include "irc_client.hpp"
# include "../utils/colors.h"

std::ostream &operator << (std::ostream &output, const IrcClient &cl);

#endif

ostream_irc_client.cpp:

#include "ostream_irc_client.hpp"
#include <sstream>

std::ostream &operator << (std::ostream &output , const IrcClient &cl)
{
    output << "NICKNAME->" << cl.getNickName() << " | ";
    output << "USERNAME->" << cl.getUserName() << " | ";
    output << "HOSTNAME->" << cl.getHostName() << " | ";
    output << "SERVERNAME->"<< cl.getServerName() << " | ";
    output << "REALNAME->"<< cl.getRealName() << " | ";
    return (output);
}

ostream_irc_server.hpp:

#ifndef OSTREAM_IRC_SERVER_HPP
# define OSTREAM_IRC_SERVER_HPP

# include <iostream>

# include "irc_server.hpp"
# include "../utils/colors.h"

std::ostream &operator << (std::ostream &output, const IrcServer &s);


#endif

ostream_irc_server.cpp:

#include "ostream_irc_server.hpp"
#include "../irc_client/ostream_irc_client.hpp"

#include <sstream>
#include <map>

std::ostream &operator << (std::ostream &output , const IrcServer &s)
{
    output << BOLD_BLUE << "server:>" << RESET << "\n";
    output << "| NICKNAME | USERNAME | HOSTNAME | SERVERNAME | STATUS |\n";
    std::map<std::string, IrcClient*>::iterator usr;
    for (usr = s.getUsers()->begin(); usr != s.getUsers()->end(); usr++)
    {
        IrcClient *cl = usr->second;
        output << *cl;
    }
    if (s.getServer().getMessages()->size() > 0)
    {
        output << "\nMessages:\n";
        std::vector<Message*>::iterator it;
        for (it = s.getServer().getMessages()->begin(); it != s.getServer().getMessages()->end(); it++)
        {
            Message *msg =  *it;
            output << msg->getData() << "\n";
        }
    }
    return (output);
}
4

0 回答 0