C++列表在范围外插入迭代器

C++ List Insert Iterator Outside Range

本文关键字:插入 迭代器 范围 列表 C++      更新时间:2023-10-16

首先,我是c++和list的新手,所以这可能很明显,我只是没有看到,对不起:(

我正试图创建一个文件列表,当我试图将第一个文件推送到列表时遇到了一个错误。调试断言错误消息为"列表插入迭代器超出范围"。

在头文件中,我有:

class ConfigDefFile
{
public:
    ConfigDefFile( const char * dir_path, const char * file_name );
private:
    ConfigDef * config;
    static char filePath[ MAX_PATH + 1 ];
};
typedef list<ConfigDefFile *> ConfigDefFileList;
ConfigDefFileList def_files;

我将文件添加到列表的代码是:

char * root_path = NULL;
ConfigDefFile * def_file;
root_path = GetDefDir(); //Gets the root directory for the file.
def_file = new ConfigDefFile( root_path, file );
def_files.push_back( def_file ); //error occurs here

关于我正在做什么会导致这种情况,有什么想法吗?

您正在按照Igor的建议破坏堆。可能在的实施中

ConfigDefFile::ConfigDefFile(const char*dir_path,const char*file_name);