jeudi 22 mars 2018

Insertion and extraction operator in C++

I have the following class in my program. I have defined insertion and extraction operator for the class.

  //Insertion operator
  friend std::ostream& operator<<(std::ostream& os, const NetworkPeerInfo& npi);

  // Extraction operator
  friend std::istream& operator>>(std::istream& is, NetworkPeerInfo& npi);

My class NetworkPeerInfo.hpp

class NetworkPeerInfo
{
public:
    NetworkPeerInfo();
    NetworkPeerInfo(std::string uuid, std::string nodeName);
    virtual ~NetworkPeerInfo();


    // Getters
    std::string UUID() const;
    std::string TerminalID() const;
    Poco::Net::IPAddress IPAddress() const;

    // Insertion operator
    friend std::ostream& operator<<(std::ostream& os, const NetworkPeerInfo& npi);

    // Extraction operator
    friend std::istream& operator>>(std::istream& is, NetworkPeerInfo& npi);

private:
    static const char *TOKEN;
    bool _isValid;
    bool _self;
    std::string _uuid;
    std::string _terminalID;
    Poco::Net::IPAddress _ipAddress;
};

My class NetworkInfo.cpp (relevant portion)

// Insertion operator
std::ostream& operator<<(std::ostream& os, const NetworkPeerInfo& npi)
{
        // write out individual members of npi with an end of line between each one
        os << npi.TOKEN << '\n';
        os << npi._isValid << '\n';
        os << npi._self << '\n';
        os << npi._uuid << '\n';
        os << npi._terminalID << '\n';
        os << npi._ipAddress << '\n';
        return os;
}

//Extraction operator
std::istream& operator>>(std::istream& is, NetworkPeerInfo& npi)
{
    // read in individual members of s
    is >> npi.TOKEN >>npi._isValid >> npi._self >> npi._uuid >> npi._terminalID >> npi._ipAddress;
    return is;
}

I am getting the following error.

error:
src/NetworkPeerInfo.cpp: In function 'std::ostream& operator<<(std::ostream&, const NetworkPeerInfo&)':
NetworkPeerInfo.cpp:52:12: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'const bool')
         os << npi._isValid << '\n';
            ^
src/NetworkPeerInfo.cpp:52:12: note: candidates are:
Src/icsContentPlayer/src/NetworkPeerInfo.cpp:48:15: note: std::ostream& operator<<(std::ostream&, const NetworkPeerInfo&)
 std::ostream& operator<<(std::ostream& os, const NetworkPeerInfo& npi)

what am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire