有我的課的矩陣操作的一部分:C++,矩陣運算,與運營商的問題
class Matrix
{
private:
std::vector < std::vector <T> > items;
const unsigned int rows_count;
const unsigned int columns_count;
public:
Matrix (unsigned int m_rows, unsigned int m_columns);
Matrix (const Matrix <T> &M);
template <typename U>
Matrix <T> & operator = (const Matrix <U> &M);
template <typename U>
bool operator == (const Matrix <U> &M) const;
template <typename U>
bool operator != (const Matrix <U> &M) const ;
template <typename U>
Matrix <T> operator + (const Matrix <U> &M) const
...
};
其中
template <typename T>
template <typename U>
Matrix <U> Matrix <T> ::operator + (const Matrix <U> &M) const
{
Matrix <U> C (M);
for (unsigned int i = 0; i < rows_count; i++)
{
for (unsigned int j = 0; j < M.getColumnsCount(); j++)
{
C (i, j) = items[i][j] + M.items[i][j];
}
}
return C;
}
template<class T>
Matrix <T> :: Matrix (const Matrix <T> &M)
: rows_count (M.rows_count), columns_count (M.columns_count), items (M.items) {}
但與以下運營商的一個問題:=
,==
,!=
我想分配矩陣A
Matrix <double> A (2,2);
Matrix <double> C (2,2);
到矩陣B
Matrix <int> B (2,2);
B = A; //Compiler error, see bellow, please
其中A和B具有不同的類型。同樣的情況發生,共同矩陣運算
C = A + B //Compiler error, see bellow, please
但是編譯器顯示此錯誤:
Error 23 error C2446: '!=' : no conversion from 'const Matrix<T> *' to 'Matrix<T> *const '
感謝您的幫助......
你實際上並沒有發佈實施,任何能產生一個錯誤的功能。 – Puppy 2011-04-25 19:54:53
@DeadMG:運營商+代碼中存在拼寫錯誤,我糾正了它... – Robo 2011-04-25 19:59:22
錯誤消息引用的指針完全缺少您提供的代碼。注意'const Matrix *'相當於'Matrix const *'而不是'Matrix * const'。 –
AProgrammer
2011-04-25 20:01:57