作业帮 > 综合 > 作业

编写一个程序实现一个矩阵类,通过重载+,-,*运算符来实现矩阵的加,减,乘操作.

来源:学生作业帮 编辑:搜狗做题网作业帮 分类:综合作业 时间:2024/06/22 11:48:23
编写一个程序实现一个矩阵类,通过重载+,-,*运算符来实现矩阵的加,减,乘操作.
最好用C++实现,
稍微有点问题,编译通不过啊,
编写一个程序实现一个矩阵类,通过重载+,-,*运算符来实现矩阵的加,减,乘操作.
// ---------------- ---------------------
// ----------------------------------------------------------------
// 假期无聊,实现的矩阵类,模板哦!哈哈.
/// ------------- ---------------
/// ---------------------------------------------------
#include
#include
#include
using namespace std;
template < class T >
class CMatrix
{
public: //------------------ 构造部 -------------------------
CMatrix( void );
CMatrix( unsigned h, unsigned w );
CMatrix( const CMatrix& m );
CMatrix( const vector& vec );
CMatrix( const vector& vec,unsigned h,unsigned w );
~CMatrix(void);
private: //------------------- 数据部 ---------------------------
vector m_vec_data;
unsigned m_u_width;
unsigned m_u_height;
public: // ------------------- 重载运算符 -------------------------
/// --- 以下均要求T具有如下运算能力:+ - += 等用到的.
//取值运算
T& operator() ( unsigned row, unsigned col );
T operator() ( unsigned row, unsigned col ) const;
//赋值运算
CMatrix& operator = ( const CMatrix& );
CMatrix& operator += ( const CMatrix& );
CMatrix& operator -= ( const CMatrix& );
CMatrix& operator *= ( T );
CMatrix& operator /= ( T );
//二元运算符
CMatrix operator + ( const CMatrix& ) const;
CMatrix operator - ( const CMatrix& ) const;
CMatrix operator * ( const CMatrix& ) const;
CMatrix operator * ( T ) const;
CMatrix operator / ( T ) const;
bool operator == ( const CMatrix& ) const;
bool operator != ( const CMatrix& ) const;
public: // -------------------- 操作函数部 -------------------------
void transpose();
inline bool empty();
inline long size();
inline unsigned height();
inline unsigned width();
public: // ------------------ 输入输出 ----------------------
// 注意:矩阵元素必须支持输入输出,否则程序会错误!
/*friend ostream& operator friend ostream& operator > ( istream& is,CMatrix& ma );
template < class T > friend istream& operator >> ( istream& is,CMatrix& ma );
};
/**-----------------------------------------------------------------------------
* 输出
*------------------------------------------------------------------------------
*/
template < class T >
ostream& operato