how to append element to a QList in a structure?

1k views Asked by At

I have a struct like this one :

struct Nom {  
    QString Nom;  
    ....  
    QList<quint64> indNum;  
}  

In my .h file. I declare :
QVector *n;

In my .cpp file. I declare :

n = new QVector<Nom>;

I read a file to fill in n. When I write this :

n->back().indNum.append(i->size()-1);  

it works.
When I write that :

n->at(j).indNum.append(i->size()-1);  

I have a compilation error:

no matching member funtion for call to 'append'
candidate function not viable: 'this' argument has type 'const QList', but method is not marked const void append(const T &t);

I don't understand why it works in the first case and the second. Could anyone explain and help me solve this ? Thanks in advance.

1

There are 1 answers

2
E4z9 On BEST ANSWER

QVector::at returns a const reference to the Nom value, so you cannot modify the item returned by n->at(j). To get a non-const reference you can use (*n)[j].

n->back() works because for QVector::back there is a const and a non-const overload.