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?
---EDIT---
It gets "Curiouser and curiouser"! this code
int utf8len(string::iterator s, string::iterator e)
{
Glib::ustring::iterator p(s);
Glib::ustring::iterator end(e);
int i=0;
for (; p != end; p++)
i++;
return i;
}
compiles and works fine.
---EDIT---
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!