QDebug () doesn't show const std :: string &
I am trying to use some vector name with struct
. I'm trying to see what name is inqDebug()
To be more clear:
const std::string& testName = "asdfqwer";
qDebug() << testName;
It gives an error in assembly:
Error: no match for 'operator<<' in 'qDebug()() << testName'
I have no parameters to change the type const std::string&
. Could you please help me to solve this problem without changing the type?
source to share
qDebug()
knows nothing about std::string
but works with const char*
. The corresponding operator can be found here . You can achieve this with data()
or with c_str()
, which is better than Jiลรญ Pospรญลกil
said.
For example:
const std::string& testName = "asdfqwer";
qDebug() << testName.data() << testName.c_str();
ASO you can convert std::string
to QString
using a QString :: fromStdString .
source to share
If you need to write std :: string to qDebug () often in your code, you can implement this function globally (for example in 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;
}
source to share