Question

I can't understand what's wrong with this part of code:

struct aliance {

    ...

    void setFirstTeam(const team& _team) {
        firstTeam = _team;
    }

    void setSecondTeam(const team& _team) {
        secondTeam = _team;
    }

}     

QVector< aliance > _aliances;  

...     

_aliances.at(i).setFirstTeam(firstAlianceTeam);
_aliances.at(i).setSecondTeam(secondAlianceTeam);

I get errors:

passing 'const aliance' as 'this' argument of 'void aliance::setFirstTeam(const team&)' discards qualifiers [-fpermissive]
passing 'const aliance' as 'this' argument of 'void aliance::setSecondTeam(const team&)' discards qualifiers [-fpermissive]

I've already retried at least all variants of these two functions with const and without one.

Was it helpful?

Solution

QVector<T>::at() returns a const T&, which means you can't call non-const member functions on the result (such as your setFirstTeam() function). Why Qt doesn't provide a non-const overload is beyond me related to Qt's copy-on-write semantics for its containers (thanks to @peppe for pointing this out). It does provide both overloads for operator[], so you should just do this instead:

_aliances[i].setFirstTeam(firstAlianceTeam);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top