Search code examples
c++qtdata-structuresqlist

Bad calling of QList::push_back()


I'm trying to insert a QList in another QList, but I'm getting the following error:

parser.y:40: error: no matching function for call to 'QList<QList<QString> >::insert(QList<QList<QString> >&)'

Trying to push a QList called partialFormula into another QList called formula:

formula.push_back(partialFormula);

I think my error is in the definition of my QList templates, because the expected value of the formula QList is a QList of QString, but I'm trying to insert a QList of QList of QString.

QList<QList<QString> > formula;
QList<QList<QString> > partialFormula;
formula.push_back(partialFormula); //error.

Also, I want to insert in partialFormula another QList >, and so on..., Do I have to create a class or a struct?

What can I do to do this?

My code:

#include <QList>
#include <QString>

int main () {
    QList<QList<QString> > formula;
    QList<QList<QString> > partialFormula;
    QList<QString> atomicCondition;

    //It works!
    partialFormula.push_back(atomicCondition);
    //It does not work.
    formula.push_back(partialFormula);

    return 0;
}

Thanks!


Solution

  • The push_back function is only there for STL-compatibility reasons and accepts only appending of variables of type T in your case QString. Have a look here: QT Doc

    Try using one of the following:

    formula.append(partialFormula);
    

    or

    formula += partialFormula;
    

    or

    formula << partialFormula;
    

    or

    formula.insert(formula.size(), partialFormula);