1
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;
}

错误出现在这句话中:

graph->matrix = ADJ_MATRIX;

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

我想将 ADJ_MATRIX 分配给struct.

4

7 回答 7

8

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...

... 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::array class, or the Boost equivalent boost::array, so it might be wise to consider those instead.

于 2012-08-07T10:05:21.990 回答
6

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

如果要复制数组,则需要将其中的每个元素一个一个复制到一个新数组中,或者使用以下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];

或者

memcpy( graph->matrix, ADJ_MATRIX, VERTEX_NUM * VERTEX_NUM * sizeof(int) );
于 2012-08-07T09:57:18.840 回答
3

数组不可赋值。您可以使用memcpy

memcpy(graph->matrix, ADJ_MATRIX, sizeof(graph->matrix));
于 2012-08-07T09:56:30.183 回答
1

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

memcpy(graph->matrix,ADJ_MATRIX,sizeof(ADJ_MATRIX));//using sizeof(graph->matrix) is safer.
于 2012-08-07T09:56:19.090 回答
1

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

于 2012-08-07T09:58:27.810 回答
0

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

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

或者

for(int i = 0; i < VERTEX_NUM; ++i){
    for(int j = 0; j < VERTEX_NUM; ++j){
        graph->matrix[i][j] = ADJ_MATRIX[i][j];
    }
}
于 2012-08-07T09:56:33.037 回答
0

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

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

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

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

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

于 2012-08-07T10:00:28.870 回答