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,347
  • 5
  • 41
  • 58
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,994
  • 1
  • 18
  • 36
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
  • 68,278
  • 7
  • 90
  • 142