尝试push_back()时,向量迭代器未解除引用错误 [英] Vector iterator not dereferencable error when trying to push_back()

查看:364
本文介绍了尝试push_back()时,向量迭代器未解除引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序因VS的错误消息而崩溃时,我遇到了一个问题:调试断言失败!(...)表达式:矢量迭代器不可解除".

I have a problem when my application crashes with this error message from VS: "Debug assertion failed! (...) Expression: vector iterator not dereferencable".

问题是,它是在使用vector的push_back期间发生的.

The thing is, it happens during using vector's push_back.

这是代码.我决定将这是我的BigInt库,作为练习来实现.该错误隐藏在我(用于测试BigInt的)创建的TestBigInt类中.公认的代码很长,但是我将bug缩小到了其中的一小部分. 这是我给错误代码的输入:

Here is the code. It is my BigInt library, that I decided to implement as an exercise. The bug is hidden in my TestBigInt class, that I created to (surprisingly) test BigInt. The code is admittedly quite long, but I narrowed the bug to a single piece of that. This is the input I give to the bugged code:

/* "BigIntTestCases.txt": format { label num1 num2 autoChecked { } }
* { 1 3 2 1 { } }
* { 2 10 7 1 { } }
* { 3 21 9 1 { } }
* ...
*/
    int main() {
        ifstream ifs{ "BigIntTestCases.txt" };
        // read tests into vector<BigIntTest>
        for (auto it = tests.begin(); it != tests.end(); ++it) {
            std::cout << "Read: " << it->label << ' ' << it->num1 << ' ' << it->num2 << ' ' << it->autoChecked << '\n';
            performTest(ofs, (*it));
        }
    }

这给了我输出:

Read: 1 3 2 1
pushed_back exResults
pushed_back outResults
Read: 2 10 7 1
pushed_back exResults
CRASH

这是"TestBigInt.cpp",这里有个错误(在第一个函数的前4个push_backs中-doTests()):

This is "TestBigInt.cpp", and here lies the bug (in the first 4 push_backs of the first function - doTests()):

void TestBigInt::doTests()
{
    // fill outResults - vector of BigInt test results
    BigInt firstNum{ num1 };
    BigInt secNum{ num2 };

    outResult.push_back((firstNum + secNum).toString());
    outResult.push_back((secNum + firstNum).toString());
    outResult.push_back((firstNum - secNum).toString());
    outResult.push_back((secNum - firstNum).toString());
    outResult.push_back((firstNum * secNum).toString());
    outResult.push_back((secNum * firstNum).toString());
    std::cout << "pushed_back outResults\n";
}
void TestBigInt::autoFillExR()
{
    // fill vector exReults -- expected results
    int firstNum = stringToI(num1);
    int secNum = stringToI(num2);

    exResult.push_back(iToString(firstNum + secNum));
    // ... - essentialy the same as doTest()
    std::cout << "pushed_back exResults\n";
}
std::ostream& performTest(std::ostream& os, TestBigInt& t)
{   
    if (t.autoChecked) // if the results are to be autochecked, than fill the exResult -- else it is already full
        t.autoFillExR();

    t.doTests();

    for (auto itE = t.exResult.cbegin(), itO = t.outResult.cbegin(); itE != t.exResult.cend() && itO != t.outResult.cend(); ++itE, ++itO)
        if (*itE != *itO)
            os << "Test not passed: " << t.label  << ", " << distance(t.exResult.cbegin(), itE) << "\n\tExpected: " << *itE << ", got " << *itO << "\n";

    return os;
}

这是BigInt :: toString的实现

This is implementation of BigInt::toString

std::string BigInt::toString() const
{
 // the digits are stored in "reversed order", i.e. digits[0] is the least significant digit
    std::string num;
    if (sign == Sign::negative)
        num += "-";
    for (auto it = digits.crbegin(); it != digits.crend(); ++it)
        num += ('0' + *it);
    return num;
}

我确实知道这是一个非常长的示例,但是至少我将bug缩小了很多.坦白地说,我不知道为什么它不起作用.

I do know that it is extremely long sample, but at least I narrowed the bug by quite a bit. Frankly I have no idea why it does not work.

非常感谢阅读这篇文章的任何人.如果您对为什么会出错有任何想法,请把它张贴在这里-我很无奈,将非常感谢您的帮助

A big thank you to anyone that read through this post. If you have any ideas about why it could be wrong, than please do post it here -- I'm quite helpless, and would appreciate any help

推荐答案

您不能在推回时遍历向量,因为push_back可能会使迭代器无效.

You can't iterate through a vector while pushing back, as push_back may invalidate iterators.

我在发布的代码中看不到上面提到的问题(参考代码),因此我收回上面的答案.在查看所有代码之前,我已经基于假设.

I can't see the problem mentioned here above in the posted code though (refer code), hence I retract the answer above. I've based it on assumptions prior to seeing all the code.

但是,给出以下代码:

for (TestBigInt t; ifs >> t; )
        tests.push_back(t);

您是否知道您始终在修改t的相同实例.您正在推送副本,但是运算符>>始终会修改原始实例,从而增加其向量.这是你想要的吗?您的搜索结果便超过了6 ...

Are you aware that you are always modifying the same instance of t. You are pushing copies, but operator >> always modifies the original instance, growing its vector. Is this what you want? Your amount of results then exceed 6...

这篇关于尝试push_back()时,向量迭代器未解除引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆