C++ 如何解决C++中“表达式必须是可修改的左值”的错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11843369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:37:37  来源:igfitidea点击:

How to solve the error "expression must be a modifiable lvalue" in c++?

c++visual-c++

提问by macemers

const int ADJ_MATRIX[VERTEX_NUM][VERTEX_NUM]={   
                                                {0,1,1,0,0,0,0,0},
                                                {1,0,0,1,1,0,0,0},
                                                {1,0,0,0,0,1,1,0},
                                                {0,1,0,0,0,0,0,1},
                                                {0,1,0,0,0,0,0,1},
                                                {0,0,1,0,0,0,1,0},
                                                {0,0,1,0,0,1,0,0},
                                                {0,0,0,1,1,0,0,0}
                                            };

typedef struct {
    int vertex;
    int matrix[VERTEX_NUM][VERTEX_NUM];
    int vNum;
    int eNum;
}Graph;

void buildGraph(Graph *graph){
    graph->vNum = VERTEX_NUM;
    graph->eNum = EDGE_NUM;
    graph->matrix = ADJ_MATRIX;
}

The error occurs in this sentence:

错误出现在这句话中:

graph->matrix = ADJ_MATRIX;

I am new to c++. please tell me why this problem occur and how to solve it?

我是 C++ 的新手。请告诉我为什么会出现这个问题以及如何解决它?

I want to assign ADJ_MATRIX to the matrix in struct.

我想将 ADJ_MATRIX 分配给struct.

回答by R. Martinho Fernandes

As was said, you can't assign arrays in C++. This is due to the compiler being a meanie, because the compiler can. It just won't let you do it...

如前所述,您不能在 C++ 中分配数组。这是因为编译器是个卑鄙的人,因为编译器可以。它只是不会让你这样做...

... unless you trick it ;)

...除非你欺骗它;)

template <typename T, int N>
struct square_matrix {
    T data[N][N];
};

square_matrix<int, 10> a;
square_matrix<int, 10> b;
a = b; // fine, and actually assigns the .data arrays
a.data = b.data; // not allowed, compiler won't let you assign arrays

The catch? Now the code needs some little things:

渔获?现在代码需要一些小东西:

const square_matrix<int, VERTEX_NUM> ADJ_MATRIX={{ 
                                               // blah blah
                                            }}; // extra set of braces

typedef struct {
    int vertex;
    square_matrix<int, VERTEX_NUM> matrix;
    int vNum;
    int eNum;
}Graph;

void buildGraph(Graph *graph){
    graph->vNum = VERTEX_NUM;
    graph->eNum = EDGE_NUM;
    graph->matrix = ADJ_MATRIX; // no change
}

And to access the cells, now we need to use graph->matrix.data[1][2]. This can be mitigated by overloading operator[]or operator()for square_matrix. However, this is now getting terribly close to the new std::arrayclass, or the Boost equivalent boost::array, so it might be wise to consider those instead.

要访问单元格,现在我们需要使用graph->matrix.data[1][2]. 这可以通过重载operator[]operator()for来缓解square_matrix。然而,这现在非常接近新std::array类,或者 Boost 等价物boost::array,所以考虑这些可能是明智的。

回答by SingerOfTheFall

Unfortunately (or maybe fortunately, who knows...) you can't just assign one array to another in C++.

不幸的是(或者幸运的是,谁知道......)你不能在 C++ 中将一个数组分配给另一个数组。

If you want to copy an array, you will need to either copy each of it's elements into a new array one by one, or use the memcpy()function:

如果要复制数组,则需要将其中的每个元素一一复制到新数组中,或者使用以下memcpy()函数:

for( int i = 0; i < VERTEX_NUM; i++ )
    for( int j = 0; j < VERTEX_NUM; j++ )  
       graph->matrix[i][j] = ADJ_MATRIX[i][j];

or

或者

memcpy( graph->matrix, ADJ_MATRIX, VERTEX_NUM * VERTEX_NUM * sizeof(int) );

回答by ecatmur

Arrays are not assignable. You can use memcpy:

数组不可分配。您可以使用memcpy

memcpy(graph->matrix, ADJ_MATRIX, sizeof(graph->matrix));

回答by perilbrain

You are trying to assign your variable address of a constant data, try using

您正在尝试分配常量数据的变量地址,请尝试使用

memcpy(graph->matrix,ADJ_MATRIX,sizeof(ADJ_MATRIX));//using sizeof(graph->matrix) is safer.

回答by mathematician1975

You cannot assign an array to another array. You will need to copy the elements from the source to the destination index by index, or use memcpyto copy the data. Array assignment like this is not allowed

您不能将一个数组分配给另一个数组。您将需要按索引将元素从源索引复制到目标索引,或用于memcpy复制数据。不允许这样的数组赋值

回答by Maksim Skurydzin

The error is thrown, because int matrix[VERTEX_NUM][VERTEX_NUM]in a structure definition means that each structure will have a 2D array of integers of the predefined size and matrixis going to be pointing to its first element. The thing is that matrixcannot be assigned to an arbitrary address, because it's a const pointer i.e. its value (the address it's pointing to) cannot change.

错误被抛出,因为int matrix[VERTEX_NUM][VERTEX_NUM]在结构定义中意味着每个结构都有一个预定义大小的二维整数数组,并且matrix将指向它的第一个元素。问题是matrix不能分配给任意地址,因为它是一个常量指针,即它的值(它指向的地址)不能改变。

You have 2 options here: you can either use memcpyor some stl algorithms to copy the ADJ_MATRIXinto matrix directly or you can declare matrixas a pointer and do the assignment that is currently produces an error.

您在这里有 2 个选项:您可以使用memcpy或 一些 stl 算法ADJ_MATRIX直接将矩阵复制到矩阵中,或者您可以声明matrix为指针并执行当前产生错误的分配。

The latter can be done in the following way:

后者可以通过以下方式完成:

typedef struct {
    int vertex;
    const int (*matrix)[VERTEX_NUM];
    int vNum;
    int eNum;
}Graph;

Thus you can do graph->matrix = ADJ_MATRIXassignment, but you won't be able to modify the individual items in matrixdue to constness. This means, graph->matrix[0][1] = 3;is not allowed, while you can read the elements freely.

因此,您可以进行graph->matrix = ADJ_MATRIX分配,但matrix由于常量性,您将无法修改单个项目。这意味着,graph->matrix[0][1] = 3;是不允许的,而您可以自由阅读元素。

回答by RiaD

You can't use an array in assignments. You may use cycles or memcpyinstead

您不能在作业中使用数组。您可以使用循环或memcpy代替

memcpy(graph->matrix, ADJ_MATRIX, VERTEX_NUM * VERTEX_NUM * sizeof(int));

or

或者

for(int i = 0; i < VERTEX_NUM; ++i){
    for(int j = 0; j < VERTEX_NUM; ++j){
        graph->matrix[i][j] = ADJ_MATRIX[i][j];
    }
}