[英]binary 'operator *' has too many parameters
我正在制作帶有模板的自定義矩陣類,用於教育目的。 我希望我的課堂以以下方式工作:
mat*2; //Works
2*mat; //Should also work
對於第一種情況,我有以下聲明
matrix<T> operator*(const T& a) const;
和實現
template <typename T>
matrix<T> matrix<T>::operator*(const T& a) const{
//Implementation here
}
對於第二種情況,我必須將實現作為非成員類進行
matrix<T> operator*(T& a, matrix<T>& mat);
與實施
template<typename T>
matrix<T> operator*(T& a, const matrix<T>& mat){
return mat*a;
}
但是,當我嘗試對此進行編譯時,使用MSVC時出現以下錯誤
error C2804: binary 'operator *' has too many parameters
..\main.cpp(33): error C2678: binary '*' : no operator found which takes a left hand operand of type 'int' (or there is no acceptable conversion)
第一個參數類型必須為T
或T const &
。
非const 左值引用不能綁定到2
類的文字(或臨時對象或const
對象或引用)。
根據我的評論:非靜態類方法具有類類型的隱式第一個參數( *this
)。 您定義的運算符(如果放置在類定義中)將具有三個參數(而二進制operator*
應該只有2個)。 如果將運算符聲明放在類定義中,則需要刪除第一個參數並直接使用類字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.