Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with wring my own iterator

Reply
Thread Tools

Problem with wring my own iterator

 
 
mast4as
Guest
Posts: n/a
 
      05-03-2010
Hi everyone

I tried to copied the code of some program that uses their own
iterator to loop over some elements of map... The code seems simple
and works in the program I copied it from of course (and I can compile
it) but I can get my own version to work. I get this error message:

xx.cc: In member function ‘A::Iterator A::begin()’:
xx.cc:716: error: conversion from
‘std::_Rb_tree_iterator<std:air<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int> >’ to non-scalar
type ‘A::Iterator’ requested
xx.cc: In member function ‘int& A::Iterator::value() const’:
xx.cc:737: error: invalid initialization of reference of type ‘int&’
from expression of type ‘const int’

Could anyone please help me ? Thank you so much -c

class A
{
public:
A() {}
typedef std::map<std::string, int> MapStuff;

class Iterator;

Iterator begin();
Iterator end();
Iterator find( const char *name );

private:

MapStuff map;
};

class A::Iterator
{
public:
Iterator();

Iterator & operator ++ ();

const std::string & name () const;
int & value() const;

private:
//friend class A::ConstIterator;

A::MapStuff::iterator _i;
};


A::Iterator
A::begin()
{
return map.begin();
}

A::Iterator::Iterator () : _i()
{
// empty
}

A::Iterator & A::Iterator:perator ++ ()
{
++_i;
return *this;
}

const std::string & A::Iterator::name () const
{
return _i->first;
}

int & A::Iterator::value() const
{
return _i->second;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-03-2010
On 5/3/2010 10:58 AM, mast4as wrote:
> Hi everyone
>
> I tried to copied the code of some program that uses their own
> iterator to loop over some elements of map... The code seems simple
> and works in the program I copied it from of course (and I can compile
> it) but I can get my own version to work. I get this error message:
>
> xx.cc: In member function ‘A::Iterator A::begin()’:
> xx.cc:716: error: conversion from
> ‘std::_Rb_tree_iterator<std:air<const std::basic_string<char,
> std::char_traits<char>, std::allocator<char> >, int> >’ to non-scalar
> type ‘A::Iterator’ requested
> xx.cc: In member function ‘int& A::Iterator::value() const’:
> xx.cc:737: error: invalid initialization of reference of type ‘int&’
> from expression of type ‘const int’
>
> Could anyone please help me ? Thank you so much -c
>
> class A
> {
> public:
> A() {}
> typedef std::map<std::string, int> MapStuff;
>
> class Iterator;
>
> Iterator begin();
> Iterator end();
> Iterator find( const char *name );
>
> private:
>
> MapStuff map;
> };
>
> class A::Iterator
> {
> public:
> Iterator();
>
> Iterator& operator ++ ();
>
> const std::string& name () const;
> int& value() const;
>
> private:
> //friend class A::ConstIterator;
>
> A::MapStuff::iterator _i;
> };
>
>
> A::Iterator
> A::begin()
> {
> return map.begin();


There does not exist a conversion between a map::iterator and your
A::Iterator. Such a conversion is required here. Did you mean to say

_i = map.begin();
return *this;

? Otherwise consider defining a constructor which takes a
'map::iterator' as its argument (by value would be OK, I suppose).

> }
>
> A::Iterator::Iterator () : _i()


You default-initialize the contained iterator. What does that do, do
you know?

> {
> // empty
> }
>
> A::Iterator& A::Iterator:perator ++ ()
> {
> ++_i;
> return *this;
> }
>
> const std::string& A::Iterator::name () const
> {
> return _i->first;
> }
>
> int& A::Iterator::value() const
> {
> return _i->second;
> }


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
mast4as
Guest
Posts: n/a
 
      05-04-2010
Thanks a lot Victor

Not sure what I was doing wrong but decided to copy the original code
from the example I had and I got it to work with a simple A class. I
believe what you mentioned about the constructor taking a
'map::iterator' as its argument was the missing elements. Anyway.
Thought I would post the working code for other people to use later
one. Thanks again for your kind help...

ps: to your question about 'what does the constructor do' when I use
_i(), nope I am not sure what it does exactly ;-( but I will try to
find out.

-c

class A
{
public:
A() : _map() {}
typedef std::map<std::string, int> AttributeMap;

class Iterator;

Iterator begin ();
Iterator end ();
Iterator find (const char name[]);

private:

AttributeMap _map;
};

//----------
// Iterators
//----------

class A::Iterator
{
public:

Iterator ();
Iterator (const A::AttributeMap::iterator &i);

Iterator & operator ++ ();
Iterator operator ++ (int);

const std::string & name () const;
int & attribute () const;

private:

//friend class A::ConstIterator;

A::AttributeMap::iterator _i;
};


//-----------------
// Inline Functions
//-----------------

A::Iterator
A::begin ()
{
return _map.begin();
}

A::Iterator
A::end ()
{
return _map.end();
}


A::Iterator
A::find (const char name[])
{
return _map.find (name);
}

inline
A::Iterator::Iterator (): _i()
{
// empty
}


inline
A::Iterator::Iterator (const A::AttributeMap::iterator &i): _i (i)
{
// empty
}


inline A::Iterator &
A::Iterator:perator ++ ()
{
++_i;
return *this;
}


inline A::Iterator
A::Iterator:perator ++ (int)
{
Iterator tmp = *this;
++_i;
return tmp;
}


inline const std::string &
A::Iterator::name () const
{
return _i->first;
}


inline int &
A::Iterator::attribute () const
{
return _i->second;
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft Aims to Wring Cash From Netbook Buyers Ablang Computer Information 0 03-16-2009 11:31 PM
what is wring with this code? =?Utf-8?B?SklNLkgu?= ASP .Net 2 05-17-2006 02:15 PM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57