Using map<> with classes

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 6
Reputation: crystalattice is an unknown quantity at this point 
Solved Threads: 0
crystalattice's Avatar
crystalattice crystalattice is offline Offline
Newbie Poster

Using map<> with classes

 
0
  #1
Nov 20th, 2004
This is kinda like "part 2" of my other post. Have also been working on this one for about 3 weeks.

A suggestion was made that I should consider using the map container to accomplish what I want. My textbook doesn't talk about maps but I found some info on the 'net. However, when I use it I get the following error:

  1. /home/cody/Projects/enumeration/src/enumeration.cpp:33: no match for ` std::ostream& << const Date&' operator
  2.  

Am I not using map correctly? (I'm using my new classes instead of fundamental types).

Prior to using the map, I had vector<T> containers for each class and output them seperately; when I changed it to a map is when the errors occurred.

I'm aware that there's a few better ways to have designed my program, but due to time constraints, I'd rather not rewrite it.
Attached Files
File Type: zip src.zip (3.3 KB, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Using map<> with classes

 
0
  #2
Nov 20th, 2004
Look at the error and tell why it has anything at all to do with map<T>.
It says you failed to define operator<< for your enumeration class.

P.S. don't post your source as attachments if you want us to look at it.
Take the relevant parts and paste those inside code tags (that means NOT to post hundreds of lines of code that does in no way relate to the problem you're having).
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6
Reputation: crystalattice is an unknown quantity at this point 
Solved Threads: 0
crystalattice's Avatar
crystalattice crystalattice is offline Offline
Newbie Poster

Re: Using map<> with classes

 
0
  #3
Nov 20th, 2004
The only reason I think it's an issue w/ the map is because the error didn't occur when using vector<T>'s or standard arrays. It only happened when I switched to map<>. I do have the << and >> operators overloaded, as shown in the last code block. This is the same implementation that I've used in previous programs w/ no problem, but it's the first time I've used it w/ map<>. That's why I don't know if I screwed up implementing it for map.

As I said, an email friend suggested using map<> instead of vector<T> to create this. My original intent was to have a vector<Date> and vector<Event> and create another vector<T> that took them both and somehow could sort them based on Date (kinda like Excel). But he told me that map containers would be easier.

  1. #include <iostream>
  2. #include <map>
  3. #include "date_class.h"
  4. #include "Events_class.h"
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. char quit;
  11. map <Date, Event> event_date;
  12. map <Date, Event>::iterator iter;
  13.  
  14. cout << "Enter the date of an event (format: mm/dd/yyyy) and the event name" << endl;
  15. do{
  16. Date d_in;
  17. Event e_in;
  18. cin >> d_in >> e_in;
  19. event_date[d_in] = e_in;
  20.  
  21. cout << "Do you have more entries? Enter 'y' or 'n'" << endl;
  22. cin >> quit;
  23. }while (quit != 'n');
  24.  
  25.  
  26. for (iter = event_date.begin(); iter != event_date.end(); iter++)
  27. cout << iter->first << "\t" << iter->second << endl;
  28. return 0;
  29. }

  1. //--------Overload output operator--------------
  2. inline ostream & operator<< (ostream & out, Date & date)
  3. {
  4. date.print(out);
  5. return out;
  6. }
  7.  
  8. //--------Overload input operator---------------
  9. inline istream & operator>> (istream & in, Date& date)
  10. {
  11. date.read(in);
  12. return in;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Using map<> with classes

 
0
  #4
Nov 20th, 2004
What does the iterator attempt to call the operator on though?

Not quite sure but I believe your iterator will return a pair<Date, Event> which will of course know of no operator<<.
You'd have to dereference the Event out of that pair using .second so you'd get iter->pair.second (or possibly iter->pair->second)

Mind that I'm not 100% certain about this, but look in that direction for your solution.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6
Reputation: crystalattice is an unknown quantity at this point 
Solved Threads: 0
crystalattice's Avatar
crystalattice crystalattice is offline Offline
Newbie Poster

Re: Using map<> with classes

 
0
  #5
Nov 20th, 2004
I'll look into that. Your right; I am trying to output a Date and Event. If I had the <<operator overloaded for both Date and Event, would that work?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Using map<> with classes

 
0
  #6
Nov 21st, 2004
you will need to overload the operator<< for both Date and Event for certain.
Whether that alone is enough to have your program do what you want you'll have to try, but likely you'll need to print both key and value manually by accessing the pair which is the data structure pointed to by the iterator and calling its "first" and "second" members (which then triggers your operators).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4882 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC