【问题标题】:debug assertion failed! expression list iterators incompatible调试断言失败!表达式列表迭代器不兼容
【发布时间】:2013-06-10 04:30:21
【问题描述】:

我正在做一个任务,我在 C++ 中实现一个字典 (ADT)。我最初用 Xcode 编写程序,一切似乎都运行良好。但是我最近发现分级机使用 Visual Studio 2010,所以我试图确保我的代码也能在 VS2010 中运行......

我现在遇到的问题是我的 remove() 哈希表函数。基本上,我使用列表向量并遍历列表以删除冲突条目。但是现在当我尝试测试这个remove() 函数时,我收到以下错误:debug assertion failed! expression list iterators incompatible。我尝试查看有关断言的 Visual C++ 文档,一切看起来都应该可以工作....有什么我忽略了吗?

这是我的代码:

///**DELETE***////  
void remove(string key) {
    size_t index = hashString(key);
    list<Entry>& entry = table.at(index);
        for(typename list<Entry>::iterator it = entry.begin();
            it != entry.end();
            /**/)
        {
            if (it->data == key) {
                table[index].erase(it);
            } else
                it++;
        }
    //entry not found
    cout << "Error: Cannot Delete... No Such Entry"<< endl;
}

【问题讨论】:

  • 断言发生在哪一行?

标签: c++ visual-studio-2010 assert visual-studio-debugging


【解决方案1】:

您忽略了erase 的返回值,这很可疑。如果您将包含erase 调用的行更改为此行,它是否有效?

it = table[index].erase(it);

或者确实:

it = entry.erase(it);

在此处查看list::erase 的文档:

http://www.cplusplus.com/reference/list/list/erase

【讨论】:

    猜你喜欢
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 2018-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多