operator 過載 注意事項

2021-06-14 19:17:19 字數 473 閱讀 8700

進行類的運算操作符過載時,需要把涉及到的運算子過載的類的標頭檔案包含近年來,例如:

#include

如果沒有包含上面標頭檔案,則下面的類進行operator 《過載時會出現編譯錯誤:binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

class a{

friend ostream& operator <<(ostream& out, a&  a);

private:

string str;

std::ostream& operator <<(std::ostream& out, a& a)

{out 《這是因為在中定義了string的"<<"過載運算子,但是沒有包含標頭檔案,所以在a中的str輸出是編譯器無法解析的。

過載operator 的注意事項

include include include using namespace std template class type,int dim class point type operator int index type operator int index const private type...

QT PaintEvent過載後注意事項

paintevent繼承過載可以實現自定義功能。子類繼承paintevent 後原定義的樣式需要在paintevent 裡重新呼叫畫刷 void mimageview paintevent qpaintevent event 注意事項 void mimageview paintevent qpain...

函式過載的注意事項 必看

include iostream using namespace std void func const int a void func int a int main 上面 中func a 呼叫那個過載函式呢?呼叫函式的優先順序取決於 傳入引數的型別與那個過載函式形參最接近 函式過載狀態一 void...