operator []重载为const引用的二维指针

时间:2014-12-03 08:22:08

标签: c++ c++11

要为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;
}

1 个答案:

答案 0 :(得分:0)

我发现你的代码已被编译(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];
}