【发布时间】:2025-11-29 02:45:01
【问题描述】:
我正在编写一个简单的 Matrix 类,并且我已经定义了一个 operator+ 重载和一个移动赋值等。 当他们两个互动时,似乎发生了一些事情,但我找不到我错在哪里。 这是我的代码(我删除了所有多余的内容,只留下了显示错误所需的内容)。 有问题的行在最后,主要是:
#include <iostream>
#define DEF -1
using namespace std;
//----- Matrix -----//
class Matrix{
private:
float **matrixpp;
int dim_r;
int dim_c;
public:
Matrix(int d_r = DEF, int d_c = 0);
Matrix(const Matrix&);
Matrix(Matrix&&);
Matrix& operator=(Matrix&&);
~Matrix();
Matrix operator+(const Matrix&);
void print();
void fill();
};
//----- Matrix -----//
Matrix::Matrix(int d_r, int d_c){
if(d_r == DEF){
do{
cout << "number of rows: ";
cin >> dim_r;
if(dim_r <= 0){
cout << "ERROR" << endl;
}
}
while(dim_r <= 0);
do{
cout << "Number of columns: ";
cin >> dim_c;
if(dim_c <= 0){
cout << "ERROR" << endl;
}
}
while(dim_c <= 0);
}
else{
dim_r = d_r;
dim_c = d_c;
}
matrixpp = new float*[dim_r];
for(int i = 0; i < dim_r; i++){
matrixpp[i] = new float[dim_c];
}
for(int r = 0; r < dim_r; r++){
for(int c = 0; c < dim_c; c++){
matrixpp[r][c] = 0;
}
}
}
Matrix::Matrix(const Matrix& tocopy)
:matrixpp(tocopy.matrixpp), dim_r(tocopy.dim_r), dim_c(tocopy.dim_c)
{
matrixpp = new float*[dim_r];
for(int i = 0; i < dim_r; i++){
matrixpp[i] = new float[dim_c];
}
for(int r = 0; r < dim_r; r++){
for(int c = 0; c < dim_c; c++){
matrixpp[r][c] = tocopy.matrixpp[r][c];
}
}
}
Matrix::Matrix(Matrix&& tomove)
:matrixpp(tomove.matrixpp), dim_r(tomove.dim_r), dim_c(tomove.dim_c)
{
tomove.matrixpp = nullptr;
}
Matrix& Matrix::operator=(Matrix&& tomove){
cout << "--- MA ---" << endl;
matrixpp = tomove.matrixpp;
dim_r = tomove.dim_r;
dim_c = tomove.dim_c;
tomove.matrixpp = nullptr;
}
Matrix::~Matrix(){
if(matrixpp != nullptr){
for(int i = 0; i < dim_r; i++){
delete[] matrixpp[i];
}
delete[] matrixpp;
}
}
Matrix Matrix::operator+(const Matrix& m){
if(this->dim_r == m.dim_r && this->dim_c == m.dim_c){
Matrix new_m(m.dim_r, m.dim_c);
for(int r = 0; r < new_m.dim_r; r++){
for(int c = 0; c < new_m.dim_c; c++){
new_m.matrixpp[r][c] = this->matrixpp[r][c] + m.matrixpp[r][c];
}
}
return new_m;
}
else{
cout << "ERROR" << endl;
}
}
void Matrix::print(){
int temp;
for(int r = 0; r < dim_r; r++){
for(int c = 0; c < dim_c; c++){
cout << matrixpp[r][c] << " ";
}
cout << endl;
}
cout << endl;
}
void Matrix::fill(){
float temp;
for(int r = 0; r < dim_r; r++){
for(int c = 0; c < dim_c; c++){
cout << "new value: " << endl;
this->print();
cin >> temp;
matrixpp[r][c] = temp;
system("clear");
}
}
}
//-------- Main -------//
int main(){
Matrix m0;
m0.fill();
Matrix m1(0);
m1 = m0+m0; // problematic line
//m1.print();
}
当调用 move assignment 将 m0+m0 的结果移动到 m1 时,它给了我错误。
如果我在 Mac 上使用 g++ 编译我的代码,给出的错误只是“非法指令:4”,仅此而已。 如果我在 Linux 上这样做,给出的错误是:
在成员函数'Matrix Matrix::operator+(const Matrix&)'中: p.cpp:90:16:错误:使用已删除的函数‘constexpr 矩阵::矩阵(常量矩阵&)' 返回new_m; ^~~~~ p.cpp:9:7: 注意:‘constexpr Matrix::Matrix(const Matrix&)’被隐式声明为已删除,因为‘Matrix’声明 移动构造函数或移动赋值运算符类 Matrix{ ^~~~~~
提前谢谢你!
【问题讨论】:
-
您在
Matrix::Matrix中的第二个do-while循环包含一个错误。您输入dim_c并检查dim_r <= 0(注意c与r)。它可能无法解决您的问题,但这仍然是一个错误。这就是复制粘贴代码的风险。另外,如果我这样做Matrix m(2, -2);会发生什么? -
请复制并粘贴编译器打印的完整错误。
-
已编辑,感谢您的错误
-
具有非
void返回类型的函数必须在每条路径上返回一些内容。你的编译器应该会警告你。
标签: c++ class operator-overloading move-assignment-operator