gpt4 book ai didi

c++ - operator[] const 引用二维指针的重载

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:51:46 25 4
gpt4 key购买 nike

要为 const 矩阵重载“+”运算符,我使用“[]”

Matrix const operator+(const Matrix & mat1, const Matrix & mat2)
{
Matrix sum(mat1);
if (sum.height != mat2.height || sum.width != mat2.width)
cout << "Dimension error!";
else {
for (int i = 0; i < sum.height; i++)
for (int j = 0; j < sum.width; j++)
sum[i][j] = mat2[i][j];
}
return sum;
}

因此,我为 [] 定义了重载

(const int*)& operator[] (int i) const {
return (*this).ele[i + 1];
}

但是IDE报错

"Error  IntelliSense: a reference of type "const int *&" (not const-qualified) cannot be initialized with a value of type "int *"

如何解决这个错误?我试过很多次TAT。

这是完整的代码:

#define NULL 0
#include<iostream>
using namespace std;

class Matrix {

private:
int width, height;
int **ele;

public:
Matrix();
Matrix(int**);
Matrix(int, int);
Matrix(const Matrix&);
Matrix operator= (const Matrix&);
friend const Matrix operator+ (const Matrix&, const Matrix&);
friend const Matrix operator- (const Matrix&, const Matrix&);
(int*)& operator[] (int i) {
return (*this).ele[i + 1];
}
(const int*)& operator[] (int i) const {
return (*this).ele[i + 1];
}
};

Matrix::Matrix() {
width = 0; height = 0;
ele = NULL;
}

Matrix::Matrix(const Matrix& mat) {
Matrix(mat.height, mat.width);
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++) {
(*this)[i][j] = mat[i][j];
}
}
Matrix::Matrix(int** e) {
(*this).height = e[0][0];
(*this).width = e[0][1];
for (int i = 1; i < height; i++)
for (int j = 0; j < width; j++) {
(*this).ele[i][j] = e[i][j];
}
}

Matrix::Matrix(int height, int width) {
(*this).width = width;
(*this).height = height;
ele = new int*[height + 1];
ele[0] = new int[2];
ele[0][0] = height; ele[0][1] = width;
for (int i = 1; i < height + 1; i++)
{
ele[i] = new int[width];
}
for (int i = 1; i < height; i++)
for (int j = 0; j < width; j++) {
ele[i][j] = 0;
}
}

inline Matrix Matrix::operator=(const Matrix & mat)
{
Matrix tempMat(mat);
return tempMat;
}

Matrix const operator+(const Matrix & mat1, const Matrix & mat2)
{
Matrix sum(mat1);
if (sum.height != mat2.height || sum.width != mat2.width)
cout << "Dimension error!";
else {
for (int i = 0; i < sum.height; i++)
for (int j = 0; j < sum.width; j++)
sum[i][j] = mat2[i][j];
}
return sum;
}

Matrix const operator-(const Matrix & mat1, const Matrix & mat2)
{
Matrix diff(mat1);
if (diff.height != mat2.height || diff.width != mat2.width)
cout << "Demension error!";
else {
for (int i = 0; i < diff.height; i++)
for (int j = 0; j < diff.width; j++)
diff[i][j] = mat2[i][j];
}
return diff;
}

最佳答案

我发现您的代码在从重载运算符签名中删除括号时已编译(g++ 版本 4.6.3),如用户 remyabel 所说。所以试试

int* operator[] (int i) {
return (*this).ele[i + 1];
}
const int* operator[] (int i) const {
return (*this).ele[i + 1];
}

代替

(int*)& operator[] (int i) {
return (*this).ele[i + 1];
}
(const int*)& operator[] (int i) const {
return (*this).ele[i + 1];
}

关于c++ - operator[] const 引用二维指针的重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27266772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com