error: cannot allocate an object of abstract type 'ClassName'



  • Hi everyone.

    I saw QRunnable examples and I want to test it, but I have an error:
    error: cannot allocate an object of abstract type 'TestRunnable'

    Why my class TestRunnable which extends QRunnable is abstract?

    Here is sample code:

    main.cpp

    #include <QCoreApplication>
    #include "testrunnable.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        
        TestRunnable *task = new TestRunnable();
    
        return a.exec();
    }
    

    testrunnable.h

    #ifndef TESTRUNNABLE_H
    #define TESTRUNNABLE_H
    
    #include <QRunnable>
    
    class TestRunnable : public QRunnable
    {
    public:
        TestRunnable();
    };
    
    #endif // TESTRUNNABLE_H
    

    testrunnable.cpp

    #include "testrunnable.h"
    
    TestRunnable::TestRunnable()
    {
    }
    
    


  • Hi
    You need to override the abstract 'run' method



  • The name 'abstract' tells the problem. Abstract classes only declare functions that need to be implemented/overwritten before you are able to allocate and use them. So read the QRunnable header file and check out all the virtual functions and overwrite them.



  • OH yes. thread without run...
    thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.