2012-11-25 19 views
1

我有以下數據結構:向量內的不可修改的值?

struct file{ 
    char name[MAX_FILE_NAME]; 
    char data[BLOCK_SIZE - MAX_FILE_NAME] 
}; 

struct disk{ 
    vector<file> current_file; 
}; 

當我嘗試修改的東西說,在我,像這樣創建的任何磁盤索引0:

disk new_disk; 

new_disk.current_file[0].name = "new name"; 

我得到的錯誤expression must be a modifiable |value

我感覺已經很晚了,我似乎無法理解一些簡單的東西......但爲什麼不能這麼做?

回答

4

你不能指定這樣的數組,例如

char foo[500]; 
foo = "this won't work"; 

你必須使用一個功能類似strcpy

strcpy(new_disk.current_file[0].name, "new name"); 

strncpy會更安全,並且std::string會更安全。

+0

+1 for'std :: string' – Xymostech