user3769519
user3769519

Reputation: 33

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?

Upvotes: 3

Views: 567

Answers (1)

Tay2510
Tay2510

Reputation: 5978

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.

Upvotes: 7

Related Questions