0

I am a student who is new to Qt. I started coding this program with Qt Concurrent functionality. The program is supposed to calculate whether a number diverges or converges to 1. I am assuming that if the length exceeds 100, it diverges.

This is my code

#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;
}

But I get build error. Any suggestions where I am going wrong?

This is the error I keep getting

main.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QtConcurrent::ThreadEngineBase::ThreadEngineBase(void)" (__imp_??0ThreadEngineBase@QtConcurrent@@QAE@XZ) referenced in function "public: __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)

hyde
  • 60,639
  • 21
  • 115
  • 176
Kamil Kamili
  • 1,757
  • 5
  • 24
  • 39
  • Could you please check that your pro file has QT += concurrent line? Also make sure you run qmake after that? – demonplus Oct 08 '16 at 10:32
  • 2
    Add `QT += concurrent` to your `.pro` file to link against the Qt concurrent module. Please remove useless information from your question, the error has nothing to do with you trying to *calculate if a function converges or diverges*. Always try to reduce the problem you are having to an [MCVE](https://stackoverflow.com/help/mcve), so that your question can be useful to others facing the same issue. Your current title has nothing to do with the error. – Mike Oct 08 '16 at 10:38
  • Why mess about with Qt? Either a function converges on an input or it does not, whilst the question can become very sophisticated, generally just run it for a hundred steps or so and see if it is converged or still wild. – Malcolm McLean Oct 08 '16 at 10:39

1 Answers1

4

As answered by @Mike, adding QT += concurrent to my .pro file worked for me.

Kamil Kamili
  • 1,757
  • 5
  • 24
  • 39
  • Accept your answer then, if this is the solution :) – demonplus Oct 10 '16 at 06:52
  • You should also never have to `#include `. You had to here because `QT += concurrent` was missing from the `.pro` file. As you've fixed it, you should now simply `#include `. For the future, if your code doesn't compile unless you do the slashed import, this clearly points to the need to add the module to the `.pro` file. – Kuba hasn't forgotten Monica Oct 10 '16 at 18:34