矩阵类在运行时出现错误

hollyren 2008-04-01 09:52:39
在程序中用到矩阵的+运算,但是出现了
binary "+" no operator defined which takes a left-hand operand of type 'class matrix<long double>'(or there is no acceptable conversion)这个错误。

当我把'+'改成'-'的时候是能编译通的,但是'+'和'-'在matrix类中的定义是一样的,这让我很不解。请高手帮帮忙阿
...全文
124 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hollyren 2008-04-01
  • 打赏
  • 举报
回复
那能不能看看是什么问题的,真的很难理解,+重载不可以,-却可以的
菜牛 2008-04-01
  • 打赏
  • 举报
回复
运算符重载建议看看Effective C++和More Effective C++,你的是有点问题。
hollyren 2008-04-01
  • 打赏
  • 举报
回复
已经把代码贴出来了,大家帮帮忙啊
hollyren 2008-04-01
  • 打赏
  • 举报
回复
template <class _Ty>
class matrix //矩阵类matrix
{
typedef matrix<_Ty> _Myt;

private:
std::valarray<_Ty> m_Datas; //定义一维数组对象m_Datas


// 赋值操作符
//矩阵与矩阵的自反*, +, -运算符
_Myt& operator += (const _Myt& rhs) //矩阵与矩阵的自反+
{
Assert(GetRowNum() == rhs.GetRowNum()); //断定两矩阵的行数相等
Assert(GetColNum() == rhs.GetColNum()); //断定两矩阵的列数相等
//利用类valarray定义,使其对象m_Datas定义进行两矩阵相加
m_Datas += rhs.m_Datas;
//结果放在左边矩阵中,返回指向左边矩阵的指针
return *this;
}
_Myt& operator -= (const _Ty& rhs) //矩阵自减数
{
m_Datas -= rhs;

return *this;
}
// 一元操作符 对矩阵(每个元素)赋予+或-号
_Myt operator + () const //赋+号
{
return *this; //不用作任何处理,维持原状
}

_Myt operator - () const //赋-号
{
_Myt mat(*this);
mat.m_Datas = -mat.m_Datas; //每个元素赋-号

return mat;
}

// 二元操作符
//矩阵加数 mat = lhs + rhs
friend _Myt operator + (const _Myt& lhs, const _Ty& rhs)
{
_Myt mat(lhs); //新生成一新矩阵对象mat
mat.m_Datas += rhs; //对新矩阵对象每个元素加数

return mat; //返回新矩阵对象
}

//矩阵减数 mat = lhs - rhs
friend _Myt operator - (const _Myt& lhs, const _Ty& rhs)
{
_Myt mat(lhs);
mat.m_Datas -= rhs;

return mat;
}
//数加矩阵 mat = lhs + rhs
friend _Myt operator + (const _Ty& lhs, const _Myt& rhs)
{
_Myt mat(rhs); //新生成一新矩阵对象mat
mat.m_Datas += lhs; //数加上新矩阵对象的每个元素

return mat;
}

//数减矩阵 mat = lhs - rhs
friend _Myt operator - (const _Ty& lhs, const _Myt& rhs)
{
_Myt mat(rhs); //新生成一新矩阵对象mat
mat.m_Datas -= lhs; //数减新矩阵对象的每个元素

return mat;
}
//矩阵加矩阵 mat = lhs + rhs
friend _Myt operator + (const _Myt& lhs, const _Myt& rhs)
{
_Myt mat(lhs); //新生成一新矩阵对象mat, 用左边阵初始化
mat.m_Datas += rhs.m_Datas; //加上右边矩阵对象每个相应元素

return mat; //返回新矩阵对象
}

//矩阵减矩阵 mat = lhs - rhs
friend _Myt operator - (const _Myt& lhs, const _Myt& rhs)
{
_Myt mat(lhs); //新生成一新矩阵对象mat, 用左边阵初始化
mat.m_Datas -= rhs.m_Datas; //减去右边矩阵对象每个相应元素

return mat; //返回新矩阵对象
}


}



这是类的代码。



void funcion()
{
matrix<long double> X1( X.GetRowNum(), X.GetColNum() );
matrix<long double> X(n, 1);
X =X+ X1;
}


函数实现大概是这样的
请高手帮忙啊
菜牛 2008-04-01
  • 打赏
  • 举报
回复
把代码贴出来
hurryboylqs 2008-04-01
  • 打赏
  • 举报
回复
你要把你的矩阵类定义发出来才知道怎么帮你解决
ouyh12345 2008-04-01
  • 打赏
  • 举报
回复
重载+运算符

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧