【问题标题】:How to work with QConcurrent class in Qt如何在 Qt 中使用 QConcurrent 类
【发布时间】:2016-10-08 10:05:06
【问题描述】:

我是一名刚接触 Qt 的学生。我开始用 Qt Concurrent 功能编写这个程序。该程序应该计算一个数字是发散还是收敛到1。我假设如果长度超过100,它就会发散。

这是我的代码

#include <QtConcurrent/QtConcurrentMap>
#include <QFuture>
#include <vector>
#include <iostream>
using namespace std;

bool converges(int &n)
{
   int count = 0;
   while (count < 100 && n > 1)
   {
        if (n % 2 == 0)
        {
           n = n/2;
        }
        else
        {
            n = 3*n+1;
        }
        if(count > 100)
        {
            break;
        }
        count = count + 1;
   }
   if (n = 1 && count <= 100)
       return true;
   else
       return false;
}

int main(int argc, char *argv[])
{
    int N = 1000000;
    vector <int> data;
    for(int i = 0; i < N; i++)
    {
        data.push_back(i);
    }
    QFuture <void> res = QtConcurrent::map(data,converges);
    res.waitForFinished();
    return 0;
}

但我得到构建错误。有什么建议我哪里出错了吗?

这是我不断收到的错误

main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QtConcurrent::ThreadEngineBase::ThreadEngineBase(void)" (__imp_??0ThreadEngineBase@QtConcurrent@@QAE@XZ) 引用在函数“公共:__thiscall QtConcurrent::IterateKernel >>,void>::IterateKernel >>,void>(class std::_Vector_iterator >>,class std::_Vector_iterator >>)”(??0?$IterateKernel@V ?$_Vector_iterator@V?$_Vector_val@U?$_Simple_types@H@std@@@std@@@std@@X@QtConcurrent@@QAE@V?$_Vector_iterator@V?$_Vector_val@U?$_Simple_types@H @std@@@std@@@std@@0@Z)

【问题讨论】:

  • 能否请您检查您的 pro 文件是否有 QT += 并发行?还要确保在那之后运行 qmake?
  • QT += concurrent 添加到您的.pro 文件以链接到Qt 并发模块。请从您的问题中删除无用信息,该错误与您尝试计算函数是收敛还是发散无关。始终尝试将您遇到的问题减少到MCVE,以便您的问题对面临相同问题的其他人有用。您当前的标题与错误无关。
  • 为什么要搞乱Qt?一个函数要么收敛于输入,要么不收敛,虽然问题可能变得非常复杂,但通常只需运行它一百步左右,看看它是否收敛或仍然很疯狂。

标签: c++ qt qtconcurrent


【解决方案1】:

正如@Mike 的回答,将QT += concurrent 添加到我的.pro 文件对我有用。

【讨论】:

  • 接受你的答案,如果这是解决方案:)
  • 您也不应该使用#include &lt;QtModule/QClass&gt;。你必须在这里,因为QT += concurrent.pro 文件中丢失。当您修复它时,您现在应该只是 #include &lt;QtConcurrentMap&gt;。将来,如果您的代码除非您执行斜杠导入否则无法编译,这清楚地表明需要将模块添加到 .pro 文件中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多