我已經構建了一個小類,它表示一個十進制數,稱爲Complex。 我希望能夠施展它的兩倍,所以這裏是我的代碼重載類型轉換不起作用
Complex.h
public:
operator double();
Complext.cpp
Complex::operator double()
{
return _num;
}
_num當然
的double類型的字段這似乎是好的。問題出在另一個類,我實際上試圖將一個複雜的對象轉換爲double。這是我的功能:
const RegMatrix& RegMatrix::operator *= (double num)
{
Complex factor(num);
for(int i=0; i<_numRow; i++)
{
for(int j=0; j<_numCol; j++)
{
Complex temp = _matrix[i][j];
_matrix[i][j] = (double) (temp * factor); //<---ERROR HERE
}
}
return *this;
}
導致invalid cast from type ‘const Complex’ to type ‘double’
我不知道爲什麼會發生。有任何想法嗎? 謝謝!