Important: Please read the Qt Code of Conduct -

Can't initialise variable of type QRegExp

  • Hey,

    I have a very simple question. I have the following code (it's a header file):

    #ifndef PARSER_HPP
    #define PARSER_HPP
    #include <QObject>
    #include <QString>
    #include <QRegExp>
    class Parser
        Parser() = default;
        Parser(QString str) : searchArgument(str) {}
        QString searchArgument;
        QRegExp rx("(\\d+)");
    #endif // PARSER_HPP

    However, on attempting to compile it, the compiler tells me that it "expected an identifier before string constant" (the QRegExp rx("(\\d+)"); part). I am kind of confused about why it doesn't work, because in the documentation it's stated that you there is a constructor of this type that takes a QString as an argument. Any ideas?

  • Lifetime Qt Champion


    When using initialisation in headers, you have to use the assignment operator not the constructor.

Log in to reply