Search code examples
c++qtqstringqtcoreqvector

How can I initialize QVector<QVector<QString>> *matrix (as class member) with new?


I really tried everything but I can't find the solution, I tried first initialize the outer QVector then the inner but it wasn't succesful.


Solution

  • Probably you can't do this, because you missed one >. So try this:

    #include<QDebug>
    //...
    private:
         QVector<QVector<QString> > *matrix = new QVector<QVector<QString> >;
    

    And in constructor:

    matrix->append(QVector<QString>() << "hello world");
    qDebug() << "output: " << *matrix;
    

    But I think that you should allocate memory in constructor. For example:

    private:
         QVector<QVector<QString> > *matrix;
    

    In constructor:

    matrix = new QVector<QVector<QString> >;
    matrix->append(QVector<QString>() << "hello world");
    qDebug() << "output:" << *matrix;
    

    Output in both cases:

    output: QVector(QVector("hello world") )