main.cpp:
Simple2DMatrix & Simple2DMatrix :: assign (const Simple2DMatrix & matrixB) {
if ((numRows == matrixB.numRows)
&& (numCols == matrixB.numCols)
)
{
for (int r = 0; r < numRows; r++)
{
for (int c = 0; c < numCols; c++)
{
this->setElement(r, c, matrixB.getElement(r, c));
}
}
return (*this);
}
else
{
throw "Dimensions does not match!";
}
}
и добавление:
if ((this->numRows == matrixB.numRows)
&& (this->numCols == matrixB.numCols)
)
{
for (int r = 0; r < this->numRows; r++)
{
for (int c = 0; c < this->numCols; c++)
{
this->setElement(r, c, this->getElement(r, c) + matrixB.getElement(r, c));
}
}
return (*this);
}
else
{
throw " Dimensions does not match!";
}
поэтому назначение будет: {
if ((numRows == matrixB.numRows)
&& (numCols == matrixB.numCols)
)
{
for (int r = 0; r < numRows; r++)
{
for (int c = 0; c < numCols; c++)
{
this->setElement(r, c, matrixB.getElement(r, c));
}
}
return (*this);
}
else
{
throw "Dimensions does not match!";
}
}
и в заголовке для operator+:
Simple2DMatrix<T> matrixTemp(matrixA.numRows, matrixA.numCols);
matrixTemp.sum(matrixA, matrixB);
return (matrixTemp);
и для оператора =:
this->assign(matrixB);
return(*this);
Большое спасибо за все ваши сообщения.
вот ваш код (operator +):
if ((this->numRows == matrixB.numRows)
&& (this->numCols == matrixB.numCols)
)
{
for (int r = 0; r < matrixA.numRows; r++)
{
for (int c = 0; c < matrixA.numCols; c++)
{
this->setElement(r, c, matrixA.getElement(r, c) + matrixB.getElement(r, c));
} //what is matrixA?
}
return (*this); //what you want to return?? it seems you are doing B=B+C
}
else
{
throw " Dimensions does not match!";
}
на самом деле вам нужно сначала создать временный объект вашего класса и сохранить в нем добавление B и C. затем верните эту временную переменную.
const
объектSimple2DMatrix
? Покажите, как вы называете оператор присваивания в вашем вопросе, пожалуйста!+
, назначение и полное сообщение об ошибке. Скорее всего, ваш оператор сложения имеет неверный тип возврата.