【问题标题】:Why does this piece of c++ not find the struct member 'filename'? [closed]为什么这段c++找不到结构成员'filename'? [关闭]
【发布时间】:2013-03-25 00:02:45
【问题描述】:
            for (torrent_info::file_iterator i = t.begin_files();
                    i != t.end_files(); ++i, ++index)
            {
                    int first = t.map_file(index, 0, 1).piece;
                    int last = t.map_file(index, i->size - 1, 1).piece;
                    std::cout << "  " << std::setw(11) << i->size
                            << " " << i.filename() << "[ " << first << ", "
                            << last << " ]\n";
            }

编译给我以下错误:

error: ‘class __gnu_cxx::__normal_iterator<const libtorrent::internal_file_entry*, std::vector<libtorrent::internal_file_entry, std::allocator<libtorrent::internal_file_entry> > >’ has no member named ‘filename’

AFAICS i 是一个常量 internal_file_entry 结构,其标头代码位于开源 libtorrent 项目中的 here。我只是第一次看 C++,但我不能 我的一生弄清楚为什么上面对i.filename() 的调用在编译时失败了?

【问题讨论】:

  • 取消引用迭代器:i-&gt;filename.
  • itorrent_info::file_iterator 而不是 internal_file_entry,它在您的代码中这样说,并且错误消息还告诉您它是一个迭代器。

标签: c++ vector struct constants


【解决方案1】:

i 是一个指向 internal_file_entry 的迭代器 - 它必须被取消引用。像这样访问filename

i->filename

这相当于:

(*i).filename

【讨论】:

    猜你喜欢
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2017-08-20
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多