By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,375 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,375 IT Pros & Developers. It's quick & easy.

C++ reading map<Object, int> from stream(file)

P: 2
I`ve got class Stock and operator >> defined for it:
Expand|Select|Wrap|Line Numbers
  1. istream &operator>>(istream &is, Stock& e){        
  2. is>>e.stockName>>e.producer>>e.date.m_day;
  3.  return is;
  4. }
Also class BunchOfStocks which stores Stock objects in a map.
Expand|Select|Wrap|Line Numbers
  1. class BunchOfStocks{
  2. map <Stock, int> stocks;
  3. public:
  4. copy (istream_iterator< Stock, int > (ifile),
  5.            istream_iterator<  Stock, int > (),
  6.        back_inserter(stocks) );
  7. };
When I try to fill a map from a file in the following BunchOfStocks class, it tells me that I don`t have operator>> which reads Stock and int defined... if I put an integer in the Stock class it would solve the problem using vector but I need to use a map.So how could this be done?Define a separate operator for the int?
Apr 21 '07 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You cannot define operators for int. All overloaded operators must have one argument that is a user-defined type.

Also, back_inserter requires a container that supports push_back and map<> does not support that.

I played around with this and got it to compile but I didn't try to test it. Let me know what you think.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <map>
  4. class Stock
  5. {
  6.         public:
  7.             Stock operator= (const Stock& rhs);
  8.             bool operator<(const Stock rhs) const ;
  9.  
  10.  
  11. };
  12.  
  13. Stock Stock::operator= (const Stock& rhs)
  14. {
  15.     return *this;  //just to get things to compile
  16. }
  17. bool Stock::operator<(const Stock rhs) const
  18. {
  19.     return true;  //just to get things to compile
  20. }
  21.  
  22. istream &operator>>(istream &is, Stock& e){        
  23. //is>>e.stockName>>e.producer>>e.date.m_day;
  24.  return is;
  25. }
  26. class BunchOfStocks
  27. {
  28.         map <Stock, int> stocks;
  29.     public:
  30.         void BunchOfStocks::Copy (istream_iterator< pair<Stock, int > >    start,
  31.            istream_iterator<  pair<Stock, int> > stop);
  32.  
  33.  
  34.  
  35. };
  36.  
  37. istream& operator>>(istream& is, pair<Stock, int>& val)
  38. {
  39.     is >> val.first >> val.second;
  40.     return is;
  41. }
  42.  
  43. void BunchOfStocks::Copy (istream_iterator< pair<Stock, int > >    start,
  44.            istream_iterator<  pair<Stock, int> > stop)
  45.  
  46. {
  47.  
  48.  
  49.     while (start != stop)
  50.     {
  51.         stocks.insert(*start);
  52.         ++start;
  53.     }   
  54. }
  55.  
  56. int main()
  57. {
  58.  
  59.     return 0;
  60.  
  61. }
  62.  
Apr 21 '07 #2

P: 2
weaknessforcats thank you very much :) I knew it was something like that with the operators overloading but you gave me what I really needed.It works fine
Apr 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.