我定义了一个矩阵类(Matirx),使用了类模板的技术
但是出现了一些问题,还请各位高手指点。定义的矩阵类
template <typename Type>
class Matrix
{
    public:
      Matrix()
       :m_pMatrix(NULL)
       ,m_rows(0)
       ,m_cols(0)
      {};
      ~Matrix();
     
     //重载操作符"=" 和 "()"
     public:
       Type operator () (const int row, const int col); //得到矩阵指定行、列的元素值
       Matrix& operator = (const Matrix& srcMat);       //赋值
      
     public:
       int Row(void) //得到矩阵的行数
       {return m_rows;};
       int Col(void) //得到矩阵的列数
       {return m_cols;};     private:
        Type** m_pMatrix;  //指向二维数组的指针
        int m_cols;     //矩阵的行数
        int        m_rows;    //矩阵的列数
}对两个重载运算符定义如下:
//返回矩阵中指定行号和列号的元素值
template <typename Type>
Type Matrix<Type>::operator () (const int row, const int col)
{
assert(m_pMatrix != NULL);
if(m_pMatrix == NULL)
{
return(9999);
}

assert((row >= 0) && (col >= 0));
if((row < 0) || (col < 0))
{
return m_pMatrix[0][0];
} assert((row < m_rows) && (col < m_cols));
if((row >= m_rows) || (col >= m_cols))
{
return m_pMatrix[m_rows - 1][m_cols - 1];
} return m_pMatrix[row][col];}//重载"=",矩阵赋值
template <typename Type> 
Matrix<Type>& Matrix<Type>::operator = (const Matrix<Type>& srcMat)
{
assert((srcMat.Row() > 0) && (srcMat.Col() > 0)); /***************************/
if((srcMat.Row() < 0) || (srcMat.Col() < 0))
return *this;

assert(!srcMat.isEmpty());
if(srcMat.isEmpty())
return *this; FreeMem();
m_rows = srcMat.Row();
m_cols = srcMat.Col(); Create(); int i = 0;
int j = 0;
for(i=0; i<m_rows; i++)
{
for(j=0; j<m_cols; j++)
{
m_pMatrix[i][j] = srcMat(i,j);
}
} return *this;
}
编译的时候总是有错
错误提示为
'Row' : cannot convert 'this' pointer from 'const class Matrix<double>' to 'class Matrix<double> &'
        Conversion loses qualifiers
        e:\备份(mxn)\lmapmathlib\matrix.h(25) : while compiling class-template member function 'class Matrix<double> &__thiscall Matrix<double>::operator =(const class Matrix<double> &)'e:\备份(mxn)\lmapmathlib\matrix.h(314) : error C2662: 'Col' : cannot convert 'this' pointer from 'const class Matrix<double>' to 'class Matrix<double> &'
        Conversion loses qualifiers
        e:\备份(mxn)\lmapmathlib\matrix.h(25) : while compiling class-template member function 'class Matrix<double> &__thiscall Matrix<double>::operator =(const class Matrix<double> &)'e:\备份(mxn)\lmapmathlib\matrix.h(315) : 
error C2662: 'Row' : cannot convert 'this' pointer from 'const class Matrix<double>' to 'class Matrix<double> &'
        Conversion loses qualifiers
        e:\备份(mxn)\lmapmathlib\matrix.h(25) : while compiling class-template member function 'class Matrix<double> &__thiscall Matrix<double>::operator =(const class Matrix<double> &)'e:\备份(mxn)\lmapmathlib\matrix.h(315) : error C2662: 'Col' : cannot convert 'this' pointer from 'const class Matrix<double>' to 'class Matrix<double> &'
这个错误是什么意思?错误指示的位置用
/***************************/
标出来了
谢谢指点!

解决方案 »

  1.   

    把Row, Col定义加 const 标志
      

  2.   

    改这个:public:
    int Row(void) const //得到矩阵的行数
    {return m_rows;};
    int Col(void) const //得到矩阵的列数
    {return m_cols;};
      

  3.   

    或者 把参数的 const 去掉。template <typename Type>
    Matrix<Type>& Matrix<Type>::operator = (Matrix<Type>& srcMat)
      

  4.   

    去掉const之后 确实就没有问题了
    为什么会是这样的呢?
      

  5.   

    因为
    Matrix<Type>& Matrix<Type>::operator = (const Matrix<Type>& srcMat)
    中,入口参数 const Matrix<Type>& srcMat 被定义为一个常量对象。
    但你调用的成员函数 Row , Col 却没有被声明为常成员函数, C++认为调用它们可能会导致 常量 对象会被修改。所以编译时不能通过。去掉const 成了普通对象,这种危险不存在了,所以编译器认为是合理的。