2017-01-01 100 views
-3

我有這樣的下面的代碼: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; 
graf1.adj = graf_adj; 
} 

在主要功能當我嘗試assing graf_adj到graf1.adj graf1.adj = graf_adj; 編者給了我這個錯誤:

Error Expression must be a modifiable lvalue

有人可以解決這個問題嗎?

謝謝立即

+1

您不能分配陣列的解決方案。您可以複製其內容,例如與'memcpy'或'std :: copy' –

+1

更好的是,使用std數組或std向量 –

+1

這個編譯('x'沒有類型) –

回答

0

已添加類型的常量:

下面是使用的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)); 
} 
相關問題