简体   繁体   中英

qDebug() not showing const std::string&

I am trying to use some vector data's name with struct . I am trying to get see which name in qDebug()

To be more clear:

const std::string& testName = "asdfqwer";
qDebug() << testName;

It gives en error message in build:

Error: no match for 'operator<<' in 'qDebug()() << testName'

I don't have options to change const std::string& type. Could you please help me to solve this issue without changing type?

qDebug() does not know anything about std::string but it works with const char* . Appropriate operator you can find here . You can achieve this with data() or with c_str() which is better as Jiří Pospíšil said.

For example:

const std::string& testName = "asdfqwer";
qDebug() << testName.data() << testName.c_str();

Aslo you can convert std::string to QString with QString::fromStdString .

If you need writing std::string to qDebug() often in your code, you can implement this function globally (for example in you main.cpp ):

#include <QDebug>
#include <string>

QDebug& operator<<(QDebug& out, const std::string& str)
{
    out << QString::fromStdString(str);
    return out;
}

int main()
{
    std::string jau = "jau";
    qDebug() << jau;
    return 0;
}

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