1

I am trying to print out the address value that I stored in the string. After I compile, it shows error: no match for 'operator<<'. Does that mean what inside the Address_list is not matched something?

#include <iostream>
#include <string>
#include <map>
#include "IPHost.h"

using namespace std;

int main() {
    map<string, IPHost> Address_list;
    Address_list["google.com"]=IPHost(74,125,225,20);
    cout << "google.com has address" << Address_list["google.com"] << endl; 
}
logoff
  • 3,229
  • 4
  • 39
  • 53
GublacK
  • 15
  • 3

2 Answers2

3
Address_list["google.com"]

would return instance of IPHost class which you stored earlier. And then it would try to apply operator<< to that instance for which it's emitting error.

This means you have not overloaded operator<< for your IPHost class.

ravi
  • 10,736
  • 1
  • 14
  • 33
1

As mentioned by others you need an overloaded operator<< for IPHost...

...which you would do like this:

inline std::ostream& operator<<(std::ostream& os, const IPHost& host)
{
   // code to stream the contents of IPHost out to os here
   // for example:
   os << "IPHost{" << host.address() << "}";

   // remember to return a reference to the ostream
   return os;
}
Richard Hodges
  • 66,334
  • 6
  • 85
  • 131