Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ QVector/Vector issues... const... discards qualifiers

Tags:

c++

qt

I setup a class called tagToken.

It has setter functions, one is void setString(QString)

I have it defined/declared as such

.h file

void setString(QString string);

Private:

    QString stringOfTagToken ;

.cpp file

void tagToken::setString(QString string)
{
    stringOfTagToken = string;
}

When I try to run this code:

    if (linePosition == 1)
    {
        QVector<tagToken> temp(0);

        //errors
        //temp.at(0).setString(line);

        temp.at(0).setString("test");

        //tags.at(0).setString(line);

        //tags.push_back();

        tagTokenCounter++;
    }

I get this error:

C:\Dev\DiffMatchPatch\diffmatchpatch.cpp:316: error: passing 'const tagToken' as 'this' argument of 'void tagToken::setString(QString)' discards qualifiers [-fpermissive] temp.at(0).setString("test");

like image 501
thistleknot Avatar asked Apr 17 '26 12:04

thistleknot


2 Answers

QVector's at function returns data as const. Use at when you don't want to (accidentally) change the vector data, or operator[] in general.

temp[0].setString("test");
like image 196
aschepler Avatar answered Apr 19 '26 01:04

aschepler


QVector::at() returns a const ref to your data, you cannot call a non-const method like setString on that

From http://qt-project.org/doc/qt-4.8/qvector.html#at

const T & QVector::at ( int i ) const
Returns the item at index position i in the vector.
i must be a valid index position in the vector (i.e., 0 <= i < size()).

Consider using operator[] instead

like image 43
tinkertime Avatar answered Apr 19 '26 02:04

tinkertime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!