繁体   English   中英

奇怪的行为或std :: map :: const_iterator

[英]Strange behavior or std::map::const_iterator

我正在创建迭代器:

typename Report::container_type_for_processed_files::const_iterator beg = rep.processed_files().begin();
            typename Report::container_type_for_processed_files::const_iterator end = rep.processed_files().end();

我的报告类看起来如下:

class Report
{
public:
typedef std::map<boost::filesystem3::path,
                 std::pair<unsigned long long/*code*/,
                           unsigned long long/*comment*/> > container_type_for_processed_files;
container_type_for_processed_files processed_files()const;
private:
container_type_for_processed_files processed_files_;
};

cpp中的已处理文件如下所示:

typename Report::container_type_for_processed_files Report::processed_files()const
{
    return processed_files_;
}

但在初始化迭代器后,如第一行所示:

typename Report::container_type_for_processed_files::const_iterator beg = rep.processed_files().begin();
            typename Report::container_type_for_processed_files::const_iterator end = rep.processed_files().end();
            while (beg != end)
            {
                qDebug() << beg->first.c_str();//here I'm getting runtime error
                fout << "File name: " << (beg->first).c_str();


                ++beg;
            }

我收到错误消息:无效的参数传递给C运行时函数。
尝试初始化迭代器时,我还在输出窗格上收到消息:
(内部错误:在psymtab中读取了pc 0x201,但在symtab中没有读取。)
这是怎么回事?

如果没有可编译的样本,我不确定这是您的问题,但这看起来很麻烦:

container_type_for_processed_files processed_files() const;

您最有可能将const&返回到那里的容器,否则该函数将返回(可能是临时的)基础容器的副本,并且您的迭代器最终将变为无效(不是对同一对象的迭代器,并且可能对临时对象的迭代器无效)生命已经结束)。

尝试:

container_type_for_processed_files const& processed_files() const;

一个问题是rep.processed_files()返回基础容器的副本 因此, begend适用于两个单独的对象; 并且尝试从一个映射的开始到另一个映射的结束进行迭代是没有意义的-它会在第一个容器的末尾进行迭代。 您应该返回对包含对象的引用。

typename Report::container_type_for_processed_files & Report::processed_files()const
{
    return processed_files_; // Returns by reference
}

当定义乞求和结束时,您需要&

typename Report::container_type_for_processed_files::const_iterator & beg = rep.processed_files().begin();
typename Report::container_type_for_processed_files::const_iterator & end = rep.processed_files().end();

正如其他人提出的那样,可能还会有其他问题。 例如,什么是qDebug

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM