我在输入“标题”和“作者”的行中遇到错误。我不太清楚该怎么做才能解决这个问题。
struct bookStruct
{
char title[40];
char author[40];
int pages;
int year;
};
enum menu { display=1, add, end} ;
void displayOptions();
void displayBooks();
int main(){
vector<bookStruct> book(11);
string option;
book[0].title = "a";
book[0].author = "b";
book[0].pages = 23;
book[0].year = 21;
displayOptions();
cin >> option;
displayBooks(book);
return 0;
}
最佳答案
你不能赋值给一个数组,你必须复制到它:
std::strcpy(book[0].title, "a");
但由于您使用的是 C++,我建议您使用 std::string
而不是字符数组:
struct bookStruct
{
std::string title;
std::string author;
int pages;
int year;
};
然后你就可以像现在这样使用普通赋值了。
关于c++ - 枚举数据和 vector 错误 : expression must be a modifiable lvalue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15808941/