【发布时间】:2017-03-25 13:38:07
【问题描述】:
我正在尝试为矩阵设置一些重载运算符函数,我使用构造函数动态分配它们,并使用 istream 运算符设置值。尝试将两个矩阵相乘时遇到问题(运行时错误)。
任何帮助将不胜感激。 ps:我对模板不熟悉。
这是我的 * 运算符函数:
matrixType operator*(matrixType m)
{
if( Rows==m.Cols && Cols==m.Rows)
{
matrixType m3(Rows,m.Cols);
for( int i=0; i<Rows;i++)
{
for( int j=0; j<Cols; j++)
{
{
for(int g=0; g<Cols;g++)
m3.matrix[i][j]+=matrix[i][g] * m.matrix[g][j];
}
}
}
return m3;
}
}
这是我的复制操作符函数:
matrixType& operator=( matrixType& m)
{
for (int i =0; i<Rows; i++)
{
for ( int j=0; j<Cols; j++)
matrix[i][j]=m.matrix[i][j];
}
return *this;
}
【问题讨论】:
-
您没有提及有关错误的任何详细信息,但最常见的问题是当您动态分配成员时不遵循“三法则”(google it)。不过最好使用
std::vector,因为这样可以让您遵循“零规则”。 -
您好,欢迎来到 StackOverflow。请花一些时间阅读帮助页面,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。更重要的是,请阅读the Stack Overflow question checklist。您可能还想了解Minimal, Complete, and Verifiable Examples。
标签: c++ matrix operator-overloading