implementing begin()

Discussion in 'C++' started by Nick Keighley, Sep 14, 2005.

  1. Hi,

    I'm trying to iterate over a container class. The class actually
    contains a std::map, so I thought all I'd have to implement begin() and
    end().

    The example code has a problem in begin()

    #include <string>
    #include <map>

    using namespace std;

    class Inventory
    {
    public:
    typedef map<string,string>::iterator iterator;

    map<string,string>::iterator& begin()
    { return map_.begin(); } // *** ERROR HERE

    private:
    map<string,string> map_;
    };

    void f (string s)
    {
    }

    int main (void)
    {
    Inventory invent;
    Inventory::iterator i;

    i = invent.begin();

    return 0;
    }



    I can post the full text of the error message but it is rather
    long. But in essense it says "cannot convert std::map...begin() to
    std::_Rb_tree_iterator..."

    I'm using gcc with the bloodshed environment.

    Any suggestions greatfully accepted!


    --
    Nick Keighley

    "Half-assed programming was a time-filler that, like knitting,
    must date to the beginning of human experience."
    "A Fire Upon The Deep" by Verne Vinge
     
    Nick Keighley, Sep 14, 2005
    #1
    1. Advertising

  2. Nick Keighley

    Kai-Uwe Bux Guest

    Nick Keighley wrote:

    > #include <string>
    > #include <map>
    >
    > using namespace std;
    >
    > class Inventory
    > {
    > public:
    > typedef map<string,string>::iterator iterator;
    >
    > map<string,string>::iterator& begin()


    Make that:

    map<string,string>::iterator begin()

    > { return map_.begin(); } // *** ERROR HERE
    >
    > private:
    > map<string,string> map_;
    > };
    >
    > void f (string s)
    > {
    > }
    >
    > int main (void)
    > {
    > Inventory invent;
    > Inventory::iterator i;
    >
    > i = invent.begin();
    >
    > return 0;
    > }



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Sep 14, 2005
    #2
    1. Advertising

  3. Kai-Uwe Bux wrote:
    > Nick Keighley wrote:


    <snip>

    > >
    > > map<string,string>::iterator& begin()

    >
    > Make that:
    >
    > map<string,string>::iterator begin()
    >
    > > { return map_.begin(); } // *** ERROR HERE


    <snip>

    yes! thankyou!


    --
    Nick Keighley
     
    Nick Keighley, Sep 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wardy

    begin prerender event

    wardy, Jun 8, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    536
    wardy
    Jun 8, 2004
  2. Bruno Alexandre

    web toolbar... where do I begin?

    Bruno Alexandre, Aug 17, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    308
    Bruno Alexandre
    Aug 17, 2004
  3. Lucas Tam
    Replies:
    0
    Views:
    769
    Lucas Tam
    Apr 13, 2005
  4. Kylin
    Replies:
    1
    Views:
    405
  5. =?Utf-8?B?SmltIEhlYXZleQ==?=

    Internal Compiler Error: stage 'BEGIN'

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Jul 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,762
    darius
    Jan 9, 2006
Loading...

Share This Page