C++ 重载运算符=不';不使用赋值进行编译
试图用一些操作的重载对矩阵类进行编码。C++ 重载运算符=不';不使用赋值进行编译,c++,c++11,matrix,C++,C++11,Matrix,试图用一些操作的重载对矩阵类进行编码。 当我试图用这个笔划编译时,一切都出了问题 result = (l_mtx + r_mtx); 我从g++得到错误: g++-g3-std=c++11-Wall-o矩阵\u类.h矩阵.cpp matrix.cpp:在函数“int main()”中: matrix.cpp:36:12:错误:调用“matrix::matrix(matrix)”时没有匹配函数 然后是这个函数的几个候选者,我真的不太明白。 我认为有复制构造函数和几个构造函数,但这不是操作符=我认
当我试图用这个笔划编译时,一切都出了问题
result = (l_mtx + r_mtx);
我从g++得到错误:g++-g3-std=c++11-Wall-o矩阵\u类.h矩阵.cpp matrix.cpp:在函数“int main()”中:
matrix.cpp:36:12:错误:调用“matrix::matrix(matrix)”时没有匹配函数 然后是这个函数的几个候选者,我真的不太明白。
我认为有复制构造函数和几个构造函数,但这不是操作符=我认为应该在那个笔划中计算 matrix_class.h:73:5:注:matrix::matrix(matrix&)[类型=double]
(参数1从“矩阵”到“矩阵&”的转换未知) ) matrix_class.h:46:5:注:matrix::matrix(int,int)[类型=double]
(候选人需要2个参数,提供1个) matrix_class.h:39:5:注意:matrix::matrix()[类型=double]
(候选人需要0个参数,提供1个) 然后是错误:
matrix_class.h:96:18:错误:初始化“matrix::operator=(matrix)[with type=double]的参数1” 我认为我不是正确的代码赋值运算符或复制构造函数,但我找不到错误所在。对不起,这个愚蠢的问题。谢谢你的关注
//copy constructor
Matrix(const Matrix<type> &org)
{
cout << "Making a copy of " << this << endl;
row = org.getRow();
column = org.getColumn();
//allocate additional space for a copy
data = new type* [row];
for (int i = 0; i < row; ++i)
{
data[i] = new type [column];
}
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
data[i][j] = org.data[i][j];
}
}
}
//复制构造函数
矩阵(常数矩阵和组织)
{
无法声明复制赋值运算符,如
Matrix<type> & operator = ( const Matrix<type> &r_mtx )
矩阵和运算符=(常数矩阵和r_mtx)
问题是临时对象可能不会绑定到非常量引用
考虑到赋值操作符应该返回对左手对象的引用
赋值运算符本质上是无效的。它不是赋值左手对象,而是创建一个临时对象。因此没有任何赋值
可以这样定义
Matrix<type> & operator = ( const Matrix<type> &r_mtx )
{
if (row == r_mtx.getRow())
{
if (column == r_mtx.getColumn())
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
data[i][j] = r_mtx[i][j];
}
}
return *this;
}
else
{
cout << "Assign error: matrix column are not equal!" << endl;
exit(EXIT_FAILURE);
}
}
else
{
cout << "Assign error: matrix rows are not equal!" << endl;
exit(EXIT_FAILURE);
}
}
矩阵和运算符=(常数矩阵和r_mtx)
{
if(row==r\u mtx.getRow())
{
if(column==r\u mtx.getColumn())
{
对于(int i=0;i
cout下面是一个更简单的建议,使用以下变量:
矩阵和运算符=(矩阵r\u mtx)//传递值
{
如果(getRow()!=r_mtx.getRow()| getColumn()!=r_mtx.getColumn())
抛出std::runtime_错误(“矩阵分配中的维度错误”);
标准::交换(数据,r_mtx.data);
归还*这个;
}
如果要允许分配,即使目标矩阵的大小不正确,也可以执行维度检查,并复制或交换行
和列
(以及任何其他成员变量)
使用throw
比exit
更可取,因为它为代码的用户提供了更多的选择。如果他们不catch
,那么它就等于退出。你的复制构造函数可能应该使用const矩阵&
。你应该将代码的相关部分复制到你的问题中,而不是链接到外部al网站。@Brian,那么我将不允许更改它fields@Alexander没错。它是一个副本。它根本不应该修改原始的。如果你有需要在副本上修改的字段,也许你应该声明它们是可变的。
?如果我在“矩阵和矩阵::运算符”的实例化中尝试更改此字段,我会出错=(const Matrix&)[with type=double]':Matrix.cpp:30:11:从此处需要Matrix_class.h:98:17:错误:将'const Matrix'作为'int Matrix::getRow()[with type=double]'的'this'参数传递,如果(row==r_mtx.getRow())@Alexander成员函数getRow()和getColumn(),则丢弃限定符[-fppermissive]应使用限定符const定义,例如int getRow()const;@Alexander,并且在一般意义上,任何不改变对象状态的方法都应始终为const
。下面是一个关于const
,何时使用,以及为什么使用的问题。
Matrix<type> & operator = ( const Matrix<type> &r_mtx )
Matrix<type> & operator = ( const Matrix<type> &r_mtx )
{
if (row == r_mtx.getRow())
{
if (column == r_mtx.getColumn())
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
data[i][j] = r_mtx[i][j];
}
}
return *this;
}
else
{
cout << "Assign error: matrix column are not equal!" << endl;
exit(EXIT_FAILURE);
}
}
else
{
cout << "Assign error: matrix rows are not equal!" << endl;
exit(EXIT_FAILURE);
}
}
Matrix<type> & operator = ( Matrix<type> r_mtx ) // pass by value
{
if ( getRow() != r_mtx.getRow() || getColumn() != r_mtx.getColumn() )
throw std::runtime_error("Wrong dimension in matrix assigment");
std::swap(data, r_mtx.data);
return *this;
}