![](/img/trans.png)
[英]Ambiguous overload of std::ostream& operator<<(std::ostream& sstr, const T& val)
[英]std::ostream& operator<<(std::ostream&, const T&) not being overridden
有時我會寫一個 class (T 說)並嘗試覆蓋 std::ostream& operator<<(std::ostream&, const T&) 但它不適用於某些類。 這是(簡化的)class 的示例,它對我不起作用。
class ConfigFile {
public:
explicit ConfigFile(const std::string& filename);
virtual ~ConfigFile();
bool saveToDisk() const;
bool loadFromDisk();
std::string getSetting(const std::string& setting, const std::string& section="Misc") const;
void setSetting(std::string value, const std::string& name, const std::string& section ="Misc", bool updateDisk = false);
inline const SettingSectionMap& getSettingMap() const {
return mSettingMap;
}
private:
std::string mSettingFileName;
SettingSectionMap mSettingMap;
#if defined(_DEBUG) || defined(DEBUG)
public:
friend std::ostream& operator<<(std::ostream& output, const ConfigFile& c) {
output << “Output the settings map here”;
return output;
}
#endif
}
我很確定顯式關鍵字會阻止轉換構造函數的情況,但它確實像它一樣,因為當我做類似的事情時
std::cout << config_ << std::endl;
它輸出類似:0x100588140。 但后來我在另一個 class 中做同樣的事情,就像下面的一樣,一切正常。
class Stats {
Stats() {};
#if defined(_DEBUG) || defined(DEBUG)
friend std::ostream& operator<<(std::ostream& output, const Stats& p) {
output << "FPS Stats: " << p.lastFPS_ << ", " << p.avgFPS_ << ", " << p.bestFPS_ << ", " << p.worstFPS_ << " (Last/Average/Best/Worst)";
return output;
};
#endif
};
謝謝你的幫助。
編輯:為了解決這個問題,我現在將以下內容添加到我的所有課程中:
#if defined(_DEBUG) || defined(DEBUG)
public:
friend std::ostream& operator<<(std::ostream& output, const ConfigFile& c);
friend std::ostream& operator<<(std::ostream& output, ConfigFile* c) {
output << *c;
return output;
}
#endif
嘗試將簽名更改為const
指針:
std::ostream& operator<<(std::ostream& output, const ConfigFile* c);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.