【发布时间】:2019-12-22 14:35:21
【问题描述】:
大家好,我需要帮助来编写这段代码:我在写名称时出现分段错误。
typedef struct employee{
char *name;
float salary;
int stage;
}
employee;
void saisie(employee* listeEmployee,int nb_employee){
listeEmployee->name=new char(50);
for(int i=0;i<nb_employee;i++){
cout<<"Enter the name of employee, his salary and the stage" <<i<<endl;
cin>>listeEmployee[i].name;
cin>>listeEmployee[i].salary;
cin>>listeEmployee[i].stage;
}
}
【问题讨论】:
-
您是否尝试过使用
std::string? -
当然我已经包含了string的包!
-
如果你被允许使用
std::string,那么永远不要使用new char(50);或char *name;,而是使用std::string name;,这样代码会更简单,更不容易出错。 -
listeEmployee->name=new char(50);是你的错误。 -
不要在 C++ 中使用
typedef struct employee {...} employee;。这在 C 中是必需的,但在 C++ 中是完全多余的。只需改用struct employee { ... };。
标签: c++ function char structure allocation