需要左值作为赋值的左操作数-数组 [英] lvalue required as left operand of assignment - Array

查看:187
本文介绍了需要左值作为赋值的左操作数-数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是错误所在的代码段,即行

Below is the snippet of code where the error lies, the line

a[i][j] = m[i][j] + w[i][j];

返回错误

需要左值作为赋值的左操作数

lvalue required as left operand of assignment

我找不到适用于数组的答案,我的矩阵定义如下:

I can't find an answer that applies to arrays, my Matrix is defined as follows:

Matrix::Matrix() {
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            coords[i][j] = 0.0f;
}

const Matrix operator+(const Matrix &m, const Matrix &w) {
    Matrix a;
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 4 ; j++)
            a[i][j] = m[i][j] + w[i][j];  // <----- error
    return a;
}

这里是运算符[] 如何通过引用退回

Here is the operator[] How do I return by reference

const Vector Matrix::operator[](int i) const{
    switch(i)
    {
    case 0: return Vector (coords[i][0], coords[i][1], coords[i][2]);
    case 1: return Vector (coords[i][0], coords[i][1], coords[i][2]);
    case 2: return Vector (coords[i][0], coords[i][1], coords[i][2]);
    }
}

推荐答案

错误实际上是需要左值作为赋值的左操作数".

The error is actually "lvalue required as left operand of assignment".

这意味着您的a[i][j]正在给您一个临时对象.当您从函数返回值时,会发生这种情况.按值返回的函数调用是 rvalue 表达式,您不能将rvalue表达式用作赋值的左操作数.您需要同时在MatrixMatrix::operator[]返回的帮助程序类上更改operator[]的实现,以便它们通过引用返回.通过引用返回的函数调用是一个左值表达式,您可以为其赋值.

It means that your a[i][j] is giving you a temporary object. This happens when you return from a function by value. A function call that returns by value is an rvalue expression, and you cannot use an rvalue expression as the left operand of an assignment. You need to change the implementation of operator[] on both the Matrix and the helper class that Matrix::operator[] returns so that they return by reference. A function call that returns by reference is an lvalue expression and will allow you to assign to it.

template <typename T>
Vector<T>& Matrix<T>::operator[](int index) { ... }
template <typename T>
T& Vector<T>::operator[](int index) { ... }

当然,这是有道理的.如果您的operator[]没有通过引用返回,那么将其赋值给它们返回的值将如何影响Matrix的内容?

Of course, this makes sense. If your operator[]s didn't return by reference, how would assigning to the value returned by them have any effect on the contents of the Matrix?

根据您的修改:

您对课程的设计有疑问. Matrix类似乎存储称为coordsfloat的3×3数组.但是,当您使用Matrix::operator[]时,它会将coords行中的值复制到Vector对象中.它们是.然后,您按值返回该Vector,这会将Vector及其包含的值复制到该函数之外.您对返回的Vector所做的任何操作只会影响该副本.

You have a problem with the design of your class. The Matrix class appears to store a 3-by-3 array of floats called coords. However, when you use Matrix::operator[], it copies the values from a row of coords into a Vector object. They are copies. You then return that Vector by value, which copies the Vector and its contained values out of the function. Anything you do to that returned Vector will only affect that copy.

除此之外,您的switch语句完全没有意义.每种情况都是完全相同的.您只需要使用i作为数组索引,而不必打开它.

In addition to this, your switch statement is totally pointless. Every case is exactly the same. You just need to use i as the array indices and not switch on it.

此外,如果您要允许呼叫operator[]的人修改Matrix的内容,则operator[]函数不能为const.

Also, if you're going to allow people that call operator[] to modify the contents of your Matrix, then the operator[] function must not be const.

有一些替代方法可以解决您的问题.第一种是只返回float*并使用Vector取消您的计划:

There are a few alternatives that will fix your problem. The first is to just return a float* instead and scrap your plan with Vector:

float* Matrix::operator[](int i) {
    return coords[i];
}

这是一个非常简单的解决方案,但确实包含传递原始指针.原始指针可以像数组一样使用,允许使用语法m[i][j].

That's a very simple solution but does involve passing a raw pointer around. The raw pointer can be used like an array, allowing you the syntax m[i][j].

您可以执行与Eigen库相同的操作,而是提供一个带有两个参数(行索引和列索引)的operator():

You could do the same as the Eigen library and instead provide a operator() that takes two arguments, the row index and column index:

float& Matrix::operator()(int i, int j) {
    return coords[i][j];
}

请注意,float通过引用返回:float&.这意味着可以在调用operator()的外部进行修改.在这里,您将使用m(i, j)索引行和列.

Note that the float is being returned by reference: float&. This means that is modifiable from outside the call to operator(). Here, you would index a row and column with m(i, j).

这篇关于需要左值作为赋值的左操作数-数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆