简体   繁体   中英

QString equivalent of .at() C++ QT

I'm trying to run the line:

out.push_back( a.at(0) );

Where out is a QVector holding integers

QVector <int> out ( 0 );

The error code that it gives me is:

error: no matching function for call to 'QVector<int>::push_back(const QChar)'
                 out.push_back( a.at(0) );
                                        ^

Which makes me believe that the .at() isn't returning the integer from that spot in the QString a, but instead the const QChar.

Is there any way that I can have it return that integer, or a better QString function that I am missing?

What's wrong?

Your a is a QString , and it's member function QString::at() always return QChar . As far as I remember, the std::string::at() in C++ returns char too rather than int . The data types in C++/Qt must be treated carefully!

Therefore, the returned QChar needs to be translated into int first in order to meet the type of your QVector<int> template.

But no worries, QChar has a member function for it: QChar::digitValue ( Returns the numeric value of the digit, or -1 if the character is not a digit. )


Solution:

Try out.push_back(a.at(0).digitValue()); instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM