1
votes

Hey I am trying to do this basic program with Qthread. I have linking error and dont know how to resolve it. Inside the main method I call operate and it gives me this error:

main.obj:-1: error: LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: void __thiscall Controller::operate(void)" (?operate@Controller@@QAEXXZ)" in Funktion "_main".

#include <QCoreApplication>
#include <QThread>
#include <QDebug>


class Worker : public QObject
{
    Q_OBJECT

public slots:
    void doWork() {
        QString result;

        for(int i= 0; i<10;i++){

           qDebug() << i <<endl;
           this->thread()->sleep(2);
        }

        emit resultReady(result);
    }

signals:
    void resultReady(const QString &result);
};



class Controller : public QObject
{
    Q_OBJECT
    QThread workerThread;
public:
    Controller() {
        Worker *worker = new Worker;
        worker->moveToThread(&workerThread);
        connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
        connect(this, &Controller::operate, worker, &Worker::doWork);
        connect(worker, &Worker::resultReady, this, &Controller::handleResults);
        workerThread.start();
    }
    ~Controller() {
        workerThread.quit();
        workerThread.wait();
    }
    public slots:
    void handleResults(const QString &);
    signals:
    void operate();
    void display(){
        qDebug() << "DISPLAYING";

        };
    };




 int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    Controller *c;

    c->operate();

   for(int i= 0; i<10;i++){

    c->display();
    a.thread()->sleep(1);

     }


      return a.exec();
   }

Do you have any idea what it could be?

1
where is your implementation for void operate() ?brettmichaelgreen
@Brett-MichaelGreen its a signal that goes to doWorkuser7031116
This {} was missing operate(){} in declarationuser7031116
signals should not have an implementation. they should only be declared.Hayt
I see with curly brackets it is not considered signal but methoduser7031116

1 Answers

0
votes

You don't emit a signal by calling it like a function. This will try to call an implementation of the operate() which there is not.

To emit a signal use

emit c->operate(); //does not work with qt4. use implementation below

while this works Qt suggest to only emit signals from inside a class though. So you should have a function inside the class call the signal like

class Controller : public QObject
{
//...
    void startWork() { emit operate(); }
//...
};