C++ 如何在c+中添加两个矩阵+;
我写了一个类,我有一个添加矩阵的问题。我知道我必须重载运算符+,但我不知道具体如何。有什么想法吗C++ 如何在c+中添加两个矩阵+;,c++,C++,我写了一个类,我有一个添加矩阵的问题。我知道我必须重载运算符+,但我不知道具体如何。有什么想法吗 class CMatrix { private: int Rows; int Columns; float* pData; public: CMatrix(void); CMatrix(int rows, int columns);
class CMatrix
{
private:
int Rows;
int Columns;
float* pData;
public:
CMatrix(void);
CMatrix(int rows, int columns);
void setElement(int row, int column, float element);
float getElement(int row, int column);
...};
istream& operator>>(istream& in, CMatrix& matrix)
{
in >> matrix.Rows;
in >> matrix.Columns;
for(int i = 0; i < matrix.Rows; i++)
for(int j = 0; j < matrix.Columns; j++)
in >> *(matrix.pData + i * matrix.Columns + j);
return in;
}
CMatrix::CMatrix(int rows, int columns)
{
Rows = rows;
Columns = columns;
pData = new float[Rows * Columns];
float* pEnd = &pData[Rows * Columns];
for(float* p = pData; p < pEnd; p++)
*p = 0.0;
}
void CMatrix::setElement(int row, int column, float element)
{
*(pData+ row * Columns + column) = element;
}
float CMatrix::getElement(int row, int column)
{
return *(pData + row * Columns + column);
}
类CMatrix
{
私人:
int行;
int列;
浮点*pData;
公众:
CMatrix(无效);
CMatrix(int行、int列);
void setElement(int行、int列、float元素);
float getElement(int行,int列);
...};
istream和运算符>>(istream和in、CMatrix和matrix)
{
在>>矩阵中。行;
在>>矩阵.列中;
对于(int i=0;i>*(matrix.pData+i*matrix.Columns+j)中;
返回;
}
CMatrix::CMatrix(int行,int列)
{
行=行;
列=列;
pData=新浮点[行*列];
float*pEnd=&pData[行*列];
对于(float*p=pData;p
我重载了运算符“”,但运算符+有问题
不幸的是,我只有不到10个名声…所以如果我写:
CMatrix operator+(const CMatrix &lhs, const CMatrix &rhs)
{
Cmatrix result (Rows,Columns);
for(int i=0; i <Rows ; i++)
for( int j=0; j<Columns; j++)
result.setElement(i,j) = lhs.getElement(i,j) + rhs.getElement(i,j);
return result;
}
int main()
{
const int n = 10, m = 5;
CMatrix m1(n, m);
CMatrix m2(n, m);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
m1.setElement(i, j, (float)(i * m + j));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
m2.setElement(i, j, (float)(i * m + j));
cout<<m1+m2; // it doesn't work
Thanks all for help, i did it at last...
CMatrix运算符+(常数CMatrix和lhs、常数CMatrix和rhs)
{
Cmatrix结果(行、列);
对于(int i=0;i除非您需要访问
CMatrix
的私有成员以执行计算,否则我建议您使用以下原型创建非成员函数:
CMatrix operator+(const CMatrix &lhs, const CMatrix &rhs)
{
// Do the work
// You may need to throw exceptions based on incompatibilities between
// matrix configurations (i.e., dimensions)
}
您还应该添加复制构造函数、析构函数和赋值运算符(可能是移动构造函数和移动赋值),或者考虑禁用这些操作。如果不使用,则如果代码使用它们,则会遇到编译器提供的缺省问题。
你必须添加一个析构函数,因为如果你没有,就会泄漏内存。
< P> C++中重载运算符的最佳方法是在ASCONGUED运算符中加载算术运算符(<代码>运算符+ < /代码>,<代码>运算符> /代码>等)(<代码>运算符+= < /代码>及其合计)。 就你而言:Matrix& operator+=(const Matrix& other)
{
/* Add operation stuff... */
return *this;
}
Matrix operator+(const Matrix& llhs , const Matrix& rhs)
{
Matrix result( lhs ); //Copy of the first operand.
result += rhs; //Add computation.
return result; //NRVO-friendly implementation
}
这种实现有两个优点:
- 是可伸缩的:实现
是基于+
的。因此,操作符的行为是一致的、可维护的(没有重复的代码)+=
- 是有效的:赋值运算符比二进制运算符更便于缓存(您使用的是同一个实例,使用两个实例并生成结果的实例),并且此实现只使用一个副本(在
),因为此代码旨在让编译器执行以下操作结果的初始化中使用的显式副本
CRTP是一个C++类成语,它是一个类继承它的模板类,作为模板论证。例如:
template<typename T>
struct Base { /* ... */ };
struct Derived : public Base<Derived> { /* ... */ };
class Foo : public EqualityHelper<Foo> //Magic!
{
friend bool operator==(const Foo& lhs , const Foo& rhs)
{
/* Equality implementation */
}
};
int main()
{
Foo a,b;
if( a != b) //Muahahahaha
{
/* ... */
}
}
这可以扩展到任何运算符。比较运算符是了解此方法威力的最佳示例。整个比较运算符集可以在其中一个运算符中实现(a>b
isb
,a b)
,等等):
模板
struct ComparisonHelper//用户提供的基于operator>的比较运算符
{
友元布尔运算符=(常量T&lhs,常量T&rhs){return!(lhs
最后,我们可以利用多重继承同时使用多个助手:
//The complete set of logical comparison operators in six mantenible lines of code!
class Foo : public EqualityHelper<Foo> , public ComparisonHelper<Foo>
{
friend bool operator==(const Foo& lhs , const Foo& rhs)
{
/* Equality implementation */
}
friedn bool operator>(const Foo& lhs , const Foo& rhs)
{
/* Comparison implementation */
}
}
//六行代码中的完整逻辑比较运算符集!
Foo类:公共EqualityHelper、公共ComparisonHelper
{
友元布尔运算符==(常量Foo&lhs、常量Foo&rhs)
{
/*平等执行*/
}
friedn-bool运算符>(常数Foo&lhs、常数Foo&rhs)
{
/*比较实现*/
}
}
使用这个方法实现了一个带有一组助手的标题:
我只会评论Michael Goldshteyn的帖子,但是我想我没有足够的声誉去做这个(LOL),所以考虑一下这篇文章的评论:
如果需要+运算符访问CMatrix类的私有数据(在本例中需要这样做),可以通过插入以下行将此函数声明为CMatrix类中的朋友:friend CMatrix operator+(const CMatrix &, const CMatrix &);
CMatrix类定义中的任意位置。这允许您在其他位置定义的重载运算符函数访问类的私有和受保护成员(如pData成员).问题到底是什么?签名还是实现。你能添加你尝试过的解决问题的方法吗?顺便说一句,在类中添加析构函数可能是一个很好的想法,如果它们大小不同,你想做什么?我的析构函数-CMatrix::~CMatrix(void){delete[]pData;}你不能从返回CMatrix的函数中返回false。想想看,这没有意义。如果函数要正常工作,它必须抛出一个异常。好的,我的错误..但我仍然不知道如何编写这个运算符+…我想这样调用这个运算符-coutok,但我仍然不知道它是如何工作的..我可以重载运算符r一般来说,但在这个例子中,我没有。
//The complete set of logical comparison operators in six mantenible lines of code!
class Foo : public EqualityHelper<Foo> , public ComparisonHelper<Foo>
{
friend bool operator==(const Foo& lhs , const Foo& rhs)
{
/* Equality implementation */
}
friedn bool operator>(const Foo& lhs , const Foo& rhs)
{
/* Comparison implementation */
}
}
friend CMatrix operator+(const CMatrix &, const CMatrix &);