home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,125 of 33,346   
   James K. Lowden to All   
   ostream_iterator for map   
   12 Apr 12 19:46:21   
   
   From: jklowden@speakeasy.net   
      
   I feel like I've asked this question before, but if so I can't find it   
   and still don't get it, so here goes....   
      
   I want to copy a std::map to std::cout.  I define operator<< for the   
   map's value_type.  I can use that operator by defererencing a the map's   
   iterator.  Why can't I compile std::copy with ostream_iterator?   
      
   The program that follows compiles and runs as is.  Change it to #if 0   
   instead, and get "no match for 'operator<<' ".  g++ says there are many   
   candidates, all standard, none mine.   
      
   The whole "no match" error is:   
   /usr/include/g++/bits/stream_iterator.h:196: error: no match for   
   'operator<<' in '*((std::ostream_iterator,   
   char, std::char_traits >*)   
   this)->std::ostream_iterator, char,   
   std::char_traits >::_M_stream << __value'   
      
   Many thanks for your insight.   
      
   --jkl   
      
   #include    
   #include    
   #include   
   using namespace std;   
      
   typedef   map the_map;   
      
   ostream& operator<<( ostream& os, const the_map::value_type& value )   
   {   
     return os << "(" << value.first << ", " << value.second << ")";   
   }   
      
   int main( int argc, char *argv[] )   
   {   
     the_map m;   
     m[1] = 1.0F;   
      
   #if 1   
     cout << *m.begin() << endl;   
   #else   
     copy( m.begin(), m.end(),   
   	ostream_iterator(cout, "\n") );   
   #endif   
     return 0;   
   }   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca