Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向量不可解引用_C++_Multithreading_Visual Studio - Fatal编程技术网

C++ 向量不可解引用

C++ 向量不可解引用,c++,multithreading,visual-studio,C++,Multithreading,Visual Studio,在查看了注释之后,我仔细查看了代码,发现了一个错误 经过一番修补,我似乎遇到了这个错误: 调试错误:向量迭代器不可取消引用 我百分之百确定它在AsingReads的向量中 这是产生错误的新添加代码: void historical::writeData(std::vector<std::vector<std::wstring>> in, const string& symbol) { std::cout << "Sending

在查看了注释之后,我仔细查看了代码,发现了一个错误

经过一番修补,我似乎遇到了这个错误:

调试错误:向量迭代器不可取消引用

我百分之百确定它在AsingReads的向量中

这是产生错误的新添加代码:

    void historical::writeData(std::vector<std::vector<std::wstring>> in, const string& symbol) {
        std::cout << "Sending data to database connector" << std::endl;
        std::vector<std::vector<std::wstring>> temp;

        while (!in.empty()) {
            for (int i = 0; i < 5; i++) {
                temp.push_back(in.back());
                in.pop_back();
            }
            assignthreads(temp, symbol);
            temp.clear();
        }

    }
    void historical::assignthreads(std::vector<std::vector<std::wstring>> partVec, const string& symbol) {
        int i = 0;
        std::thread threads[5];
        std::vector<std::vector<std::wstring>>::iterator it;
        for (it = partVec.end();
             it != partVec.begin();
             it--) {
            std::shared_ptr<database_con> sh_ptr(new database_con);
            threads[i] = std::thread(&database_con::start, sh_ptr, *it, symbol);
            partVec.pop_back();
            i++;
        }
        for (auto& th : threads) th.join();

    }
void historical::writeData(标准::向量输入、常量字符串和符号){

std::cout您第一次通过
for
-loop,
it=partVec.end()

根据定义,您不能取消引用
向量的
结束
,但可以调用:

threads[i] = std::thread(&database_con::start, sh_ptr, *it, symbol);
您想要的
for
循环可能使用了反向迭代器,如下所示:

for(auto it = rbegin(partVec); it != rend(partVec); ++it)

还有几点需要注意:

  • 通过引用传递向量:
    void assignthreads(std::vector&partVec,const string&symbol)
  • 您需要验证
    threads
    是否与
    partVec
    的大小相同。因此,请执行:
    vector threads(size(partVec))
    或在定义
    threads
    之后执行:
    assert(size(threads)==size(partVec))

  • assignthreads
    中的
    for
    循环至少有一个问题是您试图取消对向量的
    end()
    的引用

    for (it = partVec.end(); it != partVec.begin(); it--) {
        // ...
        threads[i] = std::thread(&database_con::start, sh_ptr, *it, symbol);
        //                                                    ^^^^
    }
    
    在循环的第一次迭代中,这是未定义的;调试器只是告诉您这一点

    如果要通过循环“反转”,请使用容器的
    reverse\u迭代器
    (可通过
    rbegin()
    rend()
    获得)


    旁注:通常不建议在遍历容器时修改容器(通过
    partVec.pop_back();
    )。由于您似乎对从
    向量中删除的内容不做任何处理,因此最好对内容进行迭代,然后在循环结束后调用以删除向量中的所有内容。

    很抱歉投票结束您的第一个问题,但我相信您已经从另一个广受欢迎的问题中看到了这一点您所看到的,不是调试服务。我们不会接收程序并说出如何修复它们。我们是来帮助理解的。作为程序员,您可以找到崩溃的代码行,然后提供该代码行的崩溃信息。(通常情况下,创建所述示例的过程足以为您解决此错误。)但是如果不发布,你会得到帮助。绝对好。现在我正在寻求帮助,因为我基本上是在胡说八道,因为我所做的一切似乎都是在某种错误中积累起来的。从小处开始。从你的
    main
    只运行代码的一小部分。你甚至可能需要问其他问题来弄清楚如何创建。不要强调关于这一点,一切都在学习中。只是浏览了一下抱怨代码,可能已经发现了问题。请参阅编辑。:)到目前为止,这是最有帮助的。如果您有时间,请在我接受答案之前查看编辑(当然我仍然会接受您的答案!)!谢谢,伙计!:)@Geosocker很乐意帮忙。不过,不必急着接受我的答案,谁知道今天晚些时候可能会有更好的答案。但让我们在编辑时这样做,你能从这个问题中删除它并打开一个新问题吗?如果你这样做并在这里用链接发表评论,我会来尝试回答这个问题。重点是我们希望这些问题能对其他人有所帮助,如果问题过于复杂,以至于他们无法理解被问的是什么,他们就会放弃,转而寻找新的问题。
    for (it = partVec.rbegin(); it != partVec.rend(); ++it)