我有一個類QT:錯誤重載 「<<」 運營商在Qdatastream
class budget
{
float transportation, grocery, food, stationery;
QString key;
public:
//input output functions here.
};
我創建了一個QHash>和< <運營商我的課。
QDataStream &operator <<(QDataStream &stream, budget &myclass)
{
stream<<myclass.getFood();
stream<<myclass.getGrocery();
stream<<myclass.getKey();
stream<<myclass.getStatn();
stream<<myclass.getTransport();
return stream;
}
QDataStream &operator >>(QDataStream &stream, budget &myclass)
{
float f;
QString s;
stream>>f;
myclass.addFood(f);
stream>>f;
myclass.addGrocery(f);
stream>>s;
myclass.addDate(s);
stream>>f;
myclass.addStatn(f);
stream>>f;
myclass.addTransport(f);
return stream;
}
但即使是現在,我仍然得到一個錯誤:
C:\Users\Karthik\QT\Mendrive-build-simulator-Simulator_Qt_for_MinGW_4_4__Qt_SDK__Debug........\QtSDK\Simulator\Qt\mingw\include\QtCore\qdatastream.h:381: error: no match for 'operator<<' in 'operator<<(((QDataStream&)((QDataStream*)out)), ((const QString&)((const QString*)it.QHash::const_iterator::key with Key = QString, T = budget))) << it.QHash::const_iterator::value with Key = QString, T = budget'
這究竟是爲什麼?顯然>>操作符似乎超載,我得到的錯誤僅適用於< <操作符。
謝謝。
我試着在上面開始解的答案,但是這導致的5個錯誤形式: 'C:\ Users \ Karthik \ QT \ Mendrive-build-simulator-Simulator_Qt_for_MinGW_4_4__Qt_SDK__Debug \ .. \ Mendrive \ fileoverload.h:11:error:將'const budget'作爲'float'參數傳遞給'float budget: :** getFood()**'丟棄限定符' 其中* * getFood()**被其他成員函數取代。這就是爲什麼我試圖刪除'const' – Airmine 2012-02-13 10:12:38
是因爲funcs必須返回const值。 – Neox 2012-02-13 10:27:50
是的!聲明它爲朋友功能做到了!你能否解釋一下,它是如何產生影響的?我仍然不明白爲什麼它不能作爲非朋友的功能。非常感謝! – Airmine 2012-02-13 10:58:53