PDA

View Full Version : passing 'const QList<Element>' as 'this' argument of 'QList<T>& QList<T>::operator<<(



bigkoma
20th August 2010, 12:43
Hi ,

when I compile my code, i have error :

passing 'const QList<Element>' as 'this' argument of 'QList<T>& QList<T>::operator<<(const T&) [with T = Element]' discards qualifiers

My class "Element" :

class Element
{
public:
Element();
Element(QString Aleja,QString Numer,QString Strona);
QString Aleja;
QString Numer;
QString Strona;
};


And class "Strona" :
class Strona
{
public:
Strona();
QList<Element> listElementow;
Element pobierzElement(QString NumerElementu);
Element pobierzElement(int Index);
void dodajElement(QString NrAleji,QString NrElementu,QString NrStrony) const;
};

and function where i have arror:
void Strona::dodajElement(QString NrAleji,QString NrElementu,QString NrStrony) const{
listElementow << Element(NrAleji,NrElementu,NrStrony);
}
So how a resolved this problem?

Lykurg
20th August 2010, 13:00
Don't declare dodajElement as const. Then it works. const means that no member will be changed, but you change listElementow. That is the error. If you want your method const and alter a member then use the mutable keyword.

bigkoma
20th August 2010, 13:36
Yes, it resolve this problem, but now I have another problem like this in other place :

class Aleja
{
public:
Aleja();
Aleja(QString NumerAleji);// 0-lewa,1-prawa,2-przenia
QList<Strona> Strony;
void DodajElement(QString NrElementu,QString NrStrony);
int ZwrocLiczbeElemntowNaStronie(int NrStrony);
int liczbaElementow();
QString ZwrocNumerElementu(int NrStrony,int PozycjaElementu);
QList<QString> nazwyStron();
QString NrAleji;
int lel; //liczba elementów
bool CzyElementJuzIstnieje(QString NrElementu,int NrStrony);
int PozycjaElementu(QString NrElementu,int NrStrony);
QString LatLog;
Strona pobierzStrone(QString NazwaStrony); // lewa,prawa,przednia
};

and function where i have arror:
void Aleja::DodajElement(QString NrElementu,QString NrStrony){
Strony.at(1).dodajElement(NrAleji,NrElementu,NrStr ony);
}

And similar error:
passing 'const Strona' as 'this' argument of 'void Strona::dodajElement(QString, QString, QString)' discards qualifiers

Lykurg
20th August 2010, 13:44
QList::at() returns a const value. It's the same as above. If you want to alter elements you have to use insert or replace, or better use the []operator.

bigkoma
20th August 2010, 13:54
Yes, this working and it solved my problem :)

I use the []operator.

Very, very thanks you :)