Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Trouble using 'override' keyword



  • Hello there,

    I am having trouble using the override keyword in one of my methods. Here is the code:
    @
    void NBhistoryInterface::addHistoryEntry(const QString & url) override
    {
    qDebug() << "SLOT: NBhistoryInterface::addHistoryEntry(const QString & url) override STATUS: Called";
    addHistoryItem("Title", url);
    qDebug() << "SLOT: NBhistoryInterface::addHistoryEntry(const QString & url) override STATUS: Completed";
    }
    @

    The program won't compile and gives this error:
    "expected function body after function declarator"

    What am I doing wrong? Thanks!


  • Moderators

    Have you enabled C++11 support?



  • I don't believe I have. How do I do that? I thought it was enabled be default...


  • Moderators

    It depends on your compiler. MSVC 2013 enables it by default.

    For GCC 4.8, you need to add this to your .pro file:

    @
    CONFIG += c++11
    @



  • Im using whichever compiler comes in Qt Creator


  • Lifetime Qt Champion

    Hi,

    There's no compiler coming with Qt Creator. The only "exception" if we can say, is the MinGW package which also provides the compiler.



  • Sorry here is what I mean by the compiler that comes with Qt Creator: the one that runs when I press the green arrow button on the side. (I'm pretty sure thats compiling the code). Is that the MinGW package?


  • Moderators

    When you click the green Run button, Qt Creator simply runs whatever compiler you've installed and asked it to use.

    To check the compiler you're using, click on the "Projects" button on the left.

    Also, go to Tools -> Options -> Build & Run -> Kits to see all the compiler+Qt versions available on your computer.



  • looks like we got Clang, GCC with Qt 5.2.0



  • Try, as JKSH suggested, to add @CONFIG += c++11@ to .pro file.
    If the error will stay the same then clang (whatever version you have) does not support this particular feature of C++ 11.

    If you will see a different error then probably the clang supports C++ 11.
    And the error will tell you that you put override into wrong place.
    It suppose to be in a "function declaration":http://en.cppreference.com/w/cpp/language/override and you have it in a definition.

    [EDIT] It can be specified at function definition but only if the definition is inside a class.


Log in to reply