implementing begin()

N

Nick Keighley

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
 
K

Kai-Uwe Bux

Nick said:
#include <string>
#include <map>

using namespace std;

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

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

Make that:

map said:
{ 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top