简体   繁体   中英

Strange behavior or std::map::const_iterator

I'm creating iterators:

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();

my Report class looks:

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_;
};

Processed files in cpp looks like:

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

but after initializing iterators as shown in the first lines:

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;
            }

I'm getting error: Invalid parameter passed to C runtime function.
I'm also getting messages on output pane when trying to init iterators:
(Internal error: pc 0x201 in read in psymtab, but not in symtab.)
What's going on?

Without a compilable sample, I'm not sure this is your problem, but this looks fishy:

container_type_for_processed_files processed_files() const;

You should most likely be returning a const& to the container there, otherwise that function will be returning (potentially temporary) copies of the underlying container and your iterators will end up being invalid (not iterators to the same object, and possibly iterators to temporaries whose lifetime has ended).

Try with:

container_type_for_processed_files const& processed_files() const;

One issue is that rep.processed_files() returns a copy of the underlying container. Therefore, beg and end apply to two separate objects; and it's not meaningful to try to iterate from the beginning of one map to the end of another map - it will iterate past the end of the first container. You should return a reference to the contained object.

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

And you need & when you define beg and end

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();

There may also be other issues, as raised by others. For example, what is qDebug ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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