这是我的结构
struct player {
string name;
string surname;
int goals;
string tim;
};
这是我的职责
void read(player* x, int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "************************************************" << endl;
cout << "Name:";
cin >> x->name;
cout << endl << "Surname:";
cin >> x->surname;
cout << endl << "Goals :";
cin >> x->goals;
cout << endl << "Name of the team:";
cin >> x->tim;
x++;
}
}
[当我编译时,我收到警告说:警告C26495变量'player :: goals'未初始化。始终初始化成员变量(类型6)。我不明白为什么?
您可以使用以下方法解决警告:
struct player {
string name;
string surname;
int goals = 0;
string tim;
};
具有未初始化变量的问题是,在先写入值之前先读取它们是UB。通过初始化,我们避免了该UB。