Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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
    {
        Q_OBJECT
    public:
        Parser() = default;
        Parser(QString str) : searchArgument(str) {}
    
    private:
        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

    Hi,

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


Log in to reply