can't increment Glib::ustring::iterator (getting "invalid lvalue in increment" compiler error)

Posted by davka on Stack Overflow See other posts from Stack Overflow or by davka
Published on 2010-05-30T13:10:13Z Indexed on 2010/05/30 13:12 UTC
Read the original article Hit count: 295

Filed under:
|
|

in the following code:

int utf8len(char* s, int len)
{
 Glib::ustring::iterator p( string::iterator(s) );
 Glib::ustring::iterator e ( string::iterator(s+len) );
 int i=0;
    for (; p != e; p++) // ERROR HERE!
  i++;
 return i;
}

I get the compiler error on the for line, which is sometimes "invalid lvalue in increment", and sometimes "ISO C++ forbids incrementing a pointer of type etc... ".

Yet, the follwing code:

int utf8len(char* s)
{
 Glib::ustring us(s);
 int i=0;
    for (Glib::ustring::iterator p = us.begin(); p != us.end(); p++)
  i++;
 return i;

}

compiles and works fine.

according the Glib::ustring documentation and the include file, ustring iterator can be constructed from std::string iterator, and has operator++() defined. Weird?

BONUS QUESTION :) Is there a difference in C++ between the 2 ways of defining a variable:

  classname ob1( initval );
  classname ob1 = initval;

I believed that they are synonymous; yet, if I change

   Glib::ustring::iterator p( string::iterator(s) );

to

 Glib::ustring::iterator p = string::iterator(s);

I get a compiler error (gcc 4.1.2)

conversion from ‘__gnu_cxx::__normal_iterator, std::allocator > >’ to non-scalar type ‘Glib::ustring_Iterator<__gnu_cxx::__normal_iterator, std::allocator > > >’ requesed

thanks a lot!

© Stack Overflow or respective owner

Related posts about c++

Related posts about iterator