Jorge Luis
Jorge Luis

Reputation: 955

QT application with QList<QString> function "append"

I'm trying to return a list of matches returned from a QRegularExpression to a QList with this code below:

QList<QString> list();
QString str ("something by the way");
QRegularExpression reA("pattern");
QRegularExpressionMatchIterator i = reA.globalMatch(str);

while (i.hasNext()) {
    QRegularExpressionMatch match = i.next();
    if (match.hasMatch()) {
        list.append(match.captured(0));
    }
}

return list;

...But it shows me this errors:

/home/path/.../file:line# error: request for member 'append' in 'list', which is of non-class type 'QList<QString>()'
         list.append(match.captured(0));

/home/path/.../file:line#: error: could not convert 'list' from 'QList<QString> (*)()' to 'QList<QString>'
 return list;

How can i get it working, I've tried to cast into many types.

Upvotes: 3

Views: 2598

Answers (2)

PopoLeDozo
PopoLeDozo

Reputation: 51

try the following code please:

QList<QString> list;
QString str ("something by the way");
QRegularExpression reA("pattern");
QRegularExpressionMatchIterator i = reA.globalMatch(str);

while (i.hasNext()) {
    QRegularExpressionMatch match = i.next();
    if (match.hasMatch()) {
        list.append(match.captured(0));
    }
}

return list;

Because it is possible to overload operator such as () in c++ it very complicated for your compiler to make the difference between a constructor without parameters and parenthesis operator. Because of that if you want to call a constructor without any args don't put parenthesis Qlist<QString> myList;.

You can only put parenthesis when you are using New operator QList<QString> *myList = new QList<QString>().

Parenthesis operator is used to make callable objects in C++, if you want to know more about it you can look at this link

Upvotes: 5

Krzysztof Sakowski
Krzysztof Sakowski

Reputation: 125

QList<QString> list();

It's actually a function. For a variable, you have to omit the parentheses; but this is confusing since you use usually the parentheses to pass arguments to the constructor.

It should be:

QList<QString> list;
// or
QList<QString> list{};

Upvotes: 3

Related Questions