无法递增 Glib::ustring::iterator (出现“增量中无效的左值”编译器错误)

发布于 2024-09-03 16:07:29 字数 1520 浏览 5 评论 0原文

在以下代码中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫罗兰の梦幻 2024-09-10 16:07:29

您的声明声明了此函数:

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

代码中 s 周围的括号将被忽略。它们就像下面示例中 n 周围的括号一样,

int(n);
n = 0; /* n is actually an int variable! */

它们用于对指针 (*) 或引用 (&) 等修饰符进行分组(想想关于void(*fptr)())。在您的情况下,括号在语义上只是多余的。

尝试这个:

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

引入的括号使编译器认识到它应该构造一个从表达式初始化的对象 p (因为函数参数声明不能​​有括号,所以它不会被解析为参数声明,但作为初始值设定项)。

Your declaration declares this function:

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

The parentheses in your code around s are ignored. They are like the parentheses around n in the following example

int(n);
n = 0; /* n is actually an int variable! */

They are for grouping modifiers like pointer (*) or references (&) (think about void(*fptr)()). In your case the parentheses are just semantically redundant.

Try this one instead:

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文