Thread: creating a << operator

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    creating a << operator

    I have a map <string, vector of ints> and would like to display what I hope I have loaded into the map...however, it appears that I have to define a << operator that takes a type HEADINGMAP (my map type).

    Could anyone pls assist.
    Thanks
    s

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    127
    This is the best I can do with what little detail you've provided.
    Code:
    ostream& operator<<(ostream& out, const HEADINGMAP& hmap)
    {
      // Code to write the contents of hmap to out
      return out;
    }
    When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    creating a << operator

    Thanks Kip,

    I have included your code - but am now getting compile errors...

    error C2143: syntax error : missing ',' before '&'

    does hmap have to be declared elsewhere - in a .h ?? It says it is undeclared...

    I have tried to use:
    out << *hmap->first << ": ";

    to display the string portion of the map, but am getting a compile error - error C2227: left of '->first' must point to class/struct/union

    I need to be able to access the key and values within the map - in order to write them to a report.
    Thanks for your help
    sue

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by Sue Paterniti
    Thanks Kip,

    I have included your code - but am now getting compile errors...

    error C2143: syntax error : missing ',' before '&'

    does hmap have to be declared elsewhere - in a .h ?? It says it is undeclared...

    I have tried to use:
    out << *hmap->first << ": ";

    to display the string portion of the map, but am getting a compile error - error C2227: left of '->first' must point to class/struct/union

    I need to be able to access the key and values within the map - in order to write them to a report.
    Thanks for your help
    sue
    This has all the signs of HEADINGMAP not being a type. What does the declaration of HEADINGMAP look like? I was under the impression that you were doing something like this.
    Code:
    typedef map< string, vector<int> > HEADINGMAP;
    
    [...]
    
    ostream& operator<<(ostream& out, const HEADINGMAP& hmap)
    {
      HEADINGMAP::const_iterator i = hmap.begin();
    
      for (; i != hmap.end(); ++i) {
        out << i->first << ": ";
      }
    
      return out;
    }
    When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

  5. #5
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Sue,
    You need to read up on operator overloading. Then you'll be able to make sense of Kips code and implement it into your code.

    Its not a large topic for what you need, and should help you.

    hth
    Open source isn't a matter of life or death......
    .......its much more important than that!!


    SuSE Linux - GCC 3.4.2
    XP Pro - Visual Studio 2005 TS, MinGW 3.4.2

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    Hi Kip,
    I have typedef'd an int vector as typedef vector<int> INTVEC;
    Then typdef'd the map as typedef map <string, INTVEC > HEADINGMAP;
    and an iterator as typedef HEADINGMAP::iterator HEADMAPIT;

    am still getting the compile error that there is a missing ',' before a '&' - but will do some more reading as recommended...
    thanks
    s

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Returning failure result for operator << istream
    By _Elixia_ in forum C++ Programming
    Replies: 0
    Last Post: 07-05-2003, 05:23 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM