基本上我提出在QT C++寫變量「name」的簡單函數到.txt文件,然而每當我編譯得到的錯誤消息:Qt的C++敵不過「運算符<<」流
no match for 'operator<<' (operand types are
'QTextStream' and 'std::string
{aka std::basic_string<char>}')
stream << name;
我已經看了一堆,但我不知道發生了什麼事情,任何輸入對我的錯誤將不勝感激。
功能代碼:
void Write()
{
std::string name;
QFile file("C:/Users/brandan/Desktop/GUIPrograms/Kumon.txt");
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream stream(&file); //stream of information
stream << name;
stream.flush();
file.close();
}
}
絕對沒有需要在Qt代碼中使用'std :: string',除非你需要使用'std :: string'的第三方代碼。使用'QString'。就這麼簡單。你會發現'QString'實際上有一個適用於許多真實世界用例的api,並且與處理'std :: string'所需的體操相比,生活更容易。 –