Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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++ C++;,表达式必须是可修改的左值_C++ - Fatal编程技术网

C++ C++;,表达式必须是可修改的左值

C++ C++;,表达式必须是可修改的左值,c++,C++,我有以下代码: #include "stdafx.h" #include<iostream> using namespace std; const int x = 5; bool graf_adj[x][x] = { 0,1,1,1,0, 1,0,1,0,0, 1,1,0,1,1, 1,0,1,0,0, 0,0,1,0,0 }; struct Graf { bool adj[x][x]; char n; }; int main(){ Graf graf1; gra

我有以下代码:

#include "stdafx.h"
#include<iostream>
using namespace std;

const int x = 5;
bool graf_adj[x][x] = {
0,1,1,1,0,
1,0,1,0,0,
1,1,0,1,1,
1,0,1,0,0,
0,0,1,0,0
};
struct Graf
{
    bool adj[x][x];
    char n;
};

int main(){
Graf graf1;
graf1.adj = graf_adj;
}
#包括“stdafx.h”
#包括
使用名称空间std;
常数int x=5;
bool graf_adj[x][x]={
0,1,1,1,0,
1,0,1,0,0,
1,1,0,1,1,
1,0,1,0,0,
0,0,1,0,0
};
结构图
{
bool adj[x][x];
字符n;
};
int main(){
GRAFGRAF1;
graf1.adj=graf_adj;
}
在主函数中,当我尝试将graf_adj赋值为graf1.adj时
graf1.adj=graf_adj;
complier给了我这个错误:

错误表达式必须是可修改的左值

有人能给出解决办法吗


现在您已经为常量添加了类型,谢谢您

下面是使用memcpy的解决方案

#include<iostream>
#include <cstring>
const int x = 5;
bool graf_adj[x][x] = {
0,1,1,1,0,
1,0,1,0,0,
1,1,0,1,1,
1,0,1,0,0,
0,0,1,0,0
};
struct Graf
{
    bool adj[x][x];
    char n;
};

int main(){
Graf graf1;
std::memcpy(&graf1.adj, &graf_adj, sizeof(graf1.adj));
}
#包括
#包括
常数int x=5;
bool graf_adj[x][x]={
0,1,1,1,0,
1,0,1,0,0,
1,1,0,1,1,
1,0,1,0,0,
0,0,1,0,0
};
结构图
{
bool adj[x][x];
字符n;
};
int main(){
GRAFGRAF1;
std::memcpy(&graf1.adj,&graf_adj,sizeof(graf1.adj));
}

您不能分配数组。您可以复制它们的内容,例如使用
memcpy
std::copy
更好的方法是使用std数组或std向量进行编译(
x
没有类型)