无法递增 Glib::ustring::iterator (出现“增量中无效的左值”编译器错误)
在以下代码中:
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;
}
我在 for
行上收到编译器错误,有时是“增量中的无效左值”,有时是“ISO C++ 禁止递增类型等的指针...”。
然而,以下代码:
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;
}
编译并运行良好。
根据 Glib::ustring 文档和包含文件,ustring 迭代器可以从 std::string
迭代器构造,并定义了 operator++()
。诡异的?
---编辑---
它变得“越来越好奇”!这段代码
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;
}
可以编译并且工作正常。
---编辑---
额外问题:)
C++ 中定义变量的两种方法之间是否有区别:
classname ob1( initval );
classname ob1 = initval;
我相信它们是同义的;但是,如果我更改
Glib::ustring::iterator p( string::iterator(s) );
为,
Glib::ustring::iterator p = string::iterator(s);
则会出现编译器错误(gcc 4.1.2)
转换自 '__gnu_cxx::__normal_iterator, std::分配器> >'到 非标量类型 'Glib::ustring_Iterator<__gnu_cxx::__normal_iterator, std::分配器> > >'请求
非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的声明声明了此函数:
代码中
s
周围的括号将被忽略。它们就像下面示例中n
周围的括号一样,它们用于对指针 (
*
) 或引用 (&
) 等修饰符进行分组(想想关于void(*fptr)()
)。在您的情况下,括号在语义上只是多余的。尝试这个:
引入的括号使编译器认识到它应该构造一个从表达式初始化的对象
p
(因为函数参数声明不能有括号,所以它不会被解析为参数声明,但作为初始值设定项)。Your declaration declares this function:
The parentheses in your code around
s
are ignored. They are like the parentheses aroundn
in the following exampleThey are for grouping modifiers like pointer (
*
) or references (&
) (think aboutvoid(*fptr)()
). In your case the parentheses are just semantically redundant.Try this one instead:
The parentheses introduced make the compiler regognize that it should instead construct an object
p
initialized from an expression (because a function parameter declaration can't have parentheses around it, it's not parsed as a parameter declaration, but instead as an initializer).