重载运算符 = 不使用赋值进行编译
Overloading operator= doesn't compile with assigning
github存储库与代码尝试使用重载某些操作来编写矩阵类。
当我尝试用这个笔画编译时,一切都出错
result = (l_mtx + r_mtx);
我从 g++ 收到错误:
g++ -g3 -std=c++11 -wall -o 矩阵 matrix_class.h 矩阵.cpp
matrix.cpp:在函数 'int main((' 中:
矩阵.cpp:36:12:错误:调用"矩阵::矩阵(矩阵("没有匹配函数
result = (l_mtx + r_mtx);
然后是这个函数的几个候选者,我真的不明白。
我认为有复制构造函数和几个构造函数,但这不是我认为应该在该笔画中计算的运算符=。
matrix_class.h:73:5:注意:矩阵::矩阵(矩阵&( [类型=双精度]
(参数 1 没有从"矩阵"到"矩阵"的已知转换)
matrix_class.h:46:5:注意:矩阵::矩阵(整数,整数([类型=双精度]
(考生期望 2 个参数,提供 1 个(
matrix_class.h:39:5:注意:矩阵::矩阵(( [类型 = 双精度]
(候选人期望 0 个参数,提供 1 个(
然后是错误:
matrix_class.h:96:18:错误:初始化"矩阵矩阵::运算符=(矩阵([类型=双精度]"的参数
我认为我没有正确编码分配运算符或复制构造函数,但我找不到错误在哪里。对不起,愚蠢的问题。感谢您的关注。
//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];
}
}
}
和运算符=
//assign constructor
Matrix<type> operator = (Matrix<type> r_mtx)
{
if (row == r_mtx.getRow())
{
if (column == r_mtx.getColumn())
{
//TODO: удалить прежний объект?
Matrix<type> temp(row, column);
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
temp.data[i][j] = r_mtx[i][j];
}
}
return temp;
}
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 = ( 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;
}
如果要允许分配,即使目标矩阵的大小已经不正确,则可以取出维度检查,并复制或交换row
和column
(以及任何其他成员变量(。
使用 throw
比exit
更可取,因为它为代码的用户提供了更多选项。如果他们不catch
,那么无论如何都相当于退出。
- 为"adjacent"变量赋值时出现问题
- C++中的赋值发生,尽管右侧出现异常
- 程序无法编译:将 'int (*)[3][3]' 赋值为 'int [9][3][3]' 中的不兼容类型
- 为什么在直接初始化和赋值中传递 lambda 而不是在复制初始化中传递 lambda 时会编译?
- 如何重载 const 对象的 [],以便值赋值不会导致编译错误
- 在声明上为类赋值不会编译
- 常量引用向量向量<向量<long>>赋值不编译
- 在编译时为变量赋值
- 重载运算符 = 不使用赋值进行编译
- 我无法让这段代码为赋值编译(请参阅代码后的注释)
- 值的赋值运算符未使用显式复制构造函数进行编译
- 赋值给右值:为什么要编译
- 编译-vs运行时常量变量赋值和C++中vlas的分配
- 如果尝试将负数赋值给无符号容器,是否会导致编译错误?
- 定义赋值运算符 = 只允许零 RHS,否则无法编译
- 是否有可能在编译时检测到以下赋值
- 为什么对基类的赋值有效,但对派生类的赋值是编译错误
- c++删除了移动赋值运算符的编译问题
- 当使用-O2编译时,程序跳过赋值
- 为什么C++允许在具有指针成员的类中编译默认的复制构造函数/赋值运算符重载