Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Can';t为自定义类的对象赋值,该对象的行为类似于矩阵_C++ - Fatal编程技术网

C++ Can';t为自定义类的对象赋值,该对象的行为类似于矩阵

C++ Can';t为自定义类的对象赋值,该对象的行为类似于矩阵,c++,C++,我有一个自定义类,其行为类似于矩阵。除了从同一类的其他实例中赋值外,其他操作都很正常 所以我可以做一些事情,比如: Matrix a(5,7); // du stuff with a Matrix b(5,7); Matrix d=a+b; d=a*5; d[3][2]=1; //but I can't do this: double x=d[3][2]; //at this point I get this error: main.cpp:604:12:错误:将'const Matrix

我有一个自定义类,其行为类似于矩阵。除了从同一类的其他实例中赋值外,其他操作都很正常

所以我可以做一些事情,比如:

Matrix a(5,7);
// du stuff with a
Matrix b(5,7);
Matrix d=a+b;
d=a*5;
d[3][2]=1;

//but I can't do this:
double x=d[3][2];

//at this point I get this error:
main.cpp:604:12:错误:将'const Matrix'作为'Matrix::Proxy Matrix::operator[](int)'的'this'参数传递将丢弃限定符

有人知道怎么解决这个问题吗(

我的矩阵类的实现如下:

class Matrix {
public:

Matrix(int x, int y);
~Matrix(void);
//overloaded operators
Matrix operator+(const Matrix &matrix) const;  
Matrix operator-() const;
Matrix operator-(const Matrix &matrix) const; 
Matrix operator*(const double x) const; 
Matrix operator*(const Matrix &matrix) const; 

friend istream& operator>>(istream &in, Matrix& a);

class Proxy {
    Matrix& _a;
    int _i;
public:

    Proxy(Matrix& a, int i) : _a(a), _i(i) {

    }

    double& operator[](int j) {
        return _a._arrayofarrays[_i][j];
    }
};

Proxy operator[](int i) {
    return Proxy(*this, i);
}

// copy constructor

Matrix(const Matrix& other) : _arrayofarrays() {
    _arrayofarrays = new double*[other.x ];
    for (int i = 0; i != other.x; i++)
        _arrayofarrays[i] = new double[other.y];

    for (int i = 0; i != other.x; i++)

        for (int j = 0; j != other.y; j++)
            _arrayofarrays[i][j] = other._arrayofarrays[i][j];

    x = other.x;
    y = other.y;
}
int x, y;
double** _arrayofarrays;
};

您当前有一个
操作员[]
签名:

Proxy operator[](Matrix *this, int i)
您试图将其称为:

Proxy operator[](const Matrix *, int)
错误是,为了从
const Matrix*
转换为
Matrix*
,必须丢弃
const
,这是错误的。您应该在类中提供
const
版本:

Proxy operator[](int) const {...}

在类中时,它会获得第一个参数
this
,参数列表后的
const
表示第一个参数将是指向类的常量对象的指针,而不是非常量对象。

您的
const
版本不应通过引用返回。@chris抱歉..这是以前版本的一个参数。

d它不会以任何方式影响代码的其余部分…我会编辑我的帖子并删除它..如果你的意思是
double&operator[](int j)const
你有
const
版本的
Matrix::operator[]
?这似乎与错误有关。不……这就是我在类的定义中所拥有的一切……你不介意写下来并回答,告诉我,它在代码中应该是什么样子吗?谢谢你的解释……不过当我在那里添加
代理操作符[](inti)const{return Proxy(*this,I);}
,然后它会生成以下错误:
错误:调用“矩阵::代理::代理(常量矩阵&,int&)”时没有匹配的函数你不介意,编辑你的帖子,告诉我如何处理这个错误?我对C++很陌生,所有这些东西都让我困惑:-/ Dworza,我不能用当前的代码看到一个非常简单的方法,但是如果你只想改变<代码>代理< /Cuff>类,那么这类事情就应该起作用了。(最主要的是,您可以让构造函数获取指针,然后只传递
这个
):你不介意我问你另一个与此相关的问题吗?我不知道,如果我使用你的解决方案,如何获得保存在数组中的值…当我想要打印ie时:
cout@Dworza首先,确保你的类遵循三个或五个规则(或者使用
std::vector
而不是指针)。如果在这之后仍然发生错误,请尽可能使用最小的示例来重现错误,并将其发布到新问题中。问题是,我不能使用
向量
…我在包含方面非常有限:-/好的,我将提出一个新问题:)