为什么这段 c++ 找不到结构成员"文件名"?

Why does this piece of c++ not find the struct member 'filename'?

本文关键字:成员 文件名 结构 找不到 c++ 为什么      更新时间:2023-10-16
            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项目中。我只是第一次看c++,但是我不会我的生活工作,为什么上面调用i.filename()在编译时失败?

i是指向internal_file_entry的迭代器-必须对其解引用。像这样访问filename:

i->filename

这相当于:

(*i).filename