本文介绍了qDebug()不显示const std :: string&的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些矢量数据的名称与 struct 一起使用.我试图在 qDebug()

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

更清楚:

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

它会在构建中显示错误消息:

It gives en error message in build:

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

我没有更改 const std :: string& 类型的选项.您能在不更改类型的情况下帮助我解决此问题吗?

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

推荐答案

qDebug()不了解有关 std :: string 的任何信息,但可以与一起使用const char * .您可以在此处找到合适的运营商.您可以使用 data() c_str()实现此目的,正如JiříPospíšil所说的那样.

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.

例如:

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

也可以使用std :: string 转换为 QString .html#fromStdString"rel =" noreferrer> QString :: fromStdString .

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

这篇关于qDebug()不显示const std :: string&amp;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:13