944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8211
  • C++ RSS
Nov 20th, 2004
0

Using map<> with classes

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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, 78 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crystalattice is offline Offline
6 posts
since Nov 2004
Nov 20th, 2004
0

Re: Using map<> with classes

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).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 20th, 2004
0

Re: Using map<> with classes

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.

C++ Syntax (Toggle Plain Text)
  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. }

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crystalattice is offline Offline
6 posts
since Nov 2004
Nov 20th, 2004
0

Re: Using map<> with classes

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 20th, 2004
0

Re: Using map<> with classes

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
crystalattice is offline Offline
6 posts
since Nov 2004
Nov 21st, 2004
0

Re: Using map<> with classes

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).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dealing w/ virtual functions
Next Thread in C++ Forum Timeline: array max min





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC