How to print own-class map elements

Dear Experts,
I want to store my own class (TimeTable) in the map container as a key value.

I declare a map and insert elements into the collection:

[code][size=3]map TTmap;

TimeTable t1(9,0);
TTmap[t1] = "Arrive at work";
TimeTable t3(10,30);
TTmap[t3] = "Feed dogs";[/size]

Then I want to print all key/value pair.
i use the const_iterator to walk through elements of pair.

[code][size=3]ostream& operator<< (ostream& os, map<TimeTable,string>& m1)
for(map::iterator it=m1.begin(); it!=m1.end(); it++)
cout<<(*it).first<<" << ";

But my routine couldn't print the TimeTable key elements.

I get the compiler error:

[size=2]In function `std::ostream& operator<<(std::ostream&, std::map<TimeTable, std::string, std::less<TimeTable>, std::allocator > >&)': 47
no match for 'operator<<' in 'std::cout << (&it)->std::_Rb_tree_iterator<_Tp>::operator* [with _Tp = std::pair]()->std::pair::first' [/size]

If somebody would be so kind as to explain what I print my TimeTable type key elements I'd be very grateful.


Shown below my TimeTable class:

[code]class TimeTable
int Hour;
int Minute;
friend ostream& operator<< (ostream& os, TimeTable& t);
TimeTable(int h=0,int m=0)
Hour = h;
Minute = m;

bool operator< (const TimeTable& t1) const
if (this->Hour < t1.Hour)
return true;
if (this->Hour == t1.Hour)
if (this->Minute < t1.Minute)
return true;
else return false;
else return false;

ostream& operator<< (ostream& os, TimeTable& t)
os<<t.Hour<<" : "<<t.Minute<<endl;
return os;


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!