Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why is the ranged for loop not working with the iterator defined the following way?

34 views
Skip to first unread message

emak...@gmail.com

unread,
Aug 22, 2015, 10:20:54 AM8/22/15
to


Objective is to illustrate the usage of iterators in ranged for loop. I have the following piece of code:

#include <iostream>
class str{
char *s;
int c;
public:
str(char *x): s {x} {c = 0; while(*x++)c++;}
struct iterator{
iterator(char* x) : c {x} {};
inline iterator& operator ++() {++c; return *this;}
inline bool operator == (iterator x) {return x.c == this->c;}
inline bool operator != (iterator x) {return x.c != this->c;}
inline char operator *() {return *c;}
char* c;
};


iterator begin(){return iterator(s);}
iterator end(){return iterator(s+c);}
};
int main(){
str x {(char *)"hello world"};

for(str::iterator c : x) //this gives error*. See below
std::cout << *c;
std::cout<<std::endl;

for (str::iterator c = x.begin(); c!=x.end();++c) //this works fine
std::cout<<*c;
std::cout<<std::endl;
}

I wonder why the normal for works but the ranged for does not. Can you kindly take your time and point out the error ?

errors:
MSVC: c.cpp(22): error C2440: 'initializing': cannot convert from 'char' to 'str::iterator'c.cpp(22): note: No constructor could take the source type, or constructor overload resolution was ambiguous
GCC: c.cpp:22:31: error: invalid conversion from 'char' to 'char*' [-fpermissive]

Bo Persson

unread,
Aug 22, 2015, 11:04:04 AM8/22/15
to
The range for loop will not only iterate over the range, but also
dereference the iterator for you. The message mentions that it has a
char and not an iterator.


Bo Persson

Öö Tiib

unread,
Aug 22, 2015, 11:53:46 AM8/22/15
to
On Saturday, 22 August 2015 17:20:54 UTC+3, emak...@gmail.com wrote:
> Objective is to illustrate the usage of iterators in ranged for loop. I have the following piece of code:
>
> #include <iostream>
> class str{
> char *s;
> int c;
> public:
> str(char *x): s {x} {c = 0; while(*x++)c++;}
> struct iterator{
> iterator(char* x) : c {x} {};
> inline iterator& operator ++() {++c; return *this;}
> inline bool operator == (iterator x) {return x.c == this->c;}
> inline bool operator != (iterator x) {return x.c != this->c;}
> inline char operator *() {return *c;}
> char* c;
> };
>
>
> iterator begin(){return iterator(s);}
> iterator end(){return iterator(s+c);}
> };
> int main(){
> str x {(char *)"hello world"};
>
> for(str::iterator c : x) //this gives error*. See below
> std::cout << *c;

The error diagnostic states quite clearly that you attempt to use
'str::iterator' where 'char' is available but it can't convert
'char' to 'str::iterator'. Most likely you wanted that:

for ( char c : x )
std::cout << c;

Read the description of features more carefully and also look at the
examples in book. When compiler complains about what you do but you
think that you did all correctly then it indicates that you misunderstood
how a feature works.
0 new messages