」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何重載好友運算子 `

如何重載好友運算子 `

發佈於2024-11-19
瀏覽:264

How to Overload Friend Operator `

重載範本類別的友元運算子

當嘗試將

在提供的程式碼中,

friend ostream& operator<<(ostream&, Pair<T,U>&);

但是,編譯器建議在函數名稱中新增 括號,表示應將其宣告為範本函數。正確的語法是:

friend ostream& operator<< <> (ostream&, Pair<T,U>&);

這將

記住,模板函數宣告也應該放在 Pair 類別模板定義之前,以確保編譯器在解析友元聲明時能夠識別模板函數。具有正確友元聲明和模板函數聲明的更正後的程式碼:

template <class T, class U> class Pair;

template <class T, class U>
ostream& operator<< <> (ostream&, Pair<T,U>&);

// Pair template class definition...
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3