簡體   English   中英

qDebug() 不顯示 const std::string&

[英]qDebug() not showing const std::string&

我正在嘗試將一些矢量數據的名稱與struct 我試圖在qDebug()查看哪個名稱

更清楚一點:

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

它在構建中給出錯誤消息:

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

我沒有更改const std::string&類型的選項。 你能幫我在不改變類型的情況下解決這個問題嗎?

qDebug()std::string一無所知,但它適用於const char* 您可以在此處找到合適的運算符。 您可以使用data()c_str()來實現這一點,正如Jiří Pospíšil所說,后者更好。

例如:

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

同樣,您可以使用QString::fromStdStringstd::string轉換為QString

如果您需要經常在代碼中將 std::string 寫入 qDebug() ,您可以全局實現此函數(例如在您的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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM