简体   繁体   English

如果结构数组多于一个,如何用初始化列表定义结构变量的成员?

[英]How do I define members of structure variable with an initialization list if there are more than one array of structures?

struct Birthdate {
int Month;
int Year;
int Day;
};

struct Pet{
int ID;
Birthdate date_of_birth; 
int age;
string gender;
bool status;
}; Pet Information[10];     //Number of Pets whose information are needed


Pet Information[0]={ 1, 4, 2016, 28, 3, "Male" , false  };  //Conflicting declaration 'Pet Information[0]' occurs. 

Can you not define variable with an initialization list like this? 您不能用这样的初始化列表定义变量吗?

Also, can I define bool value for status within the initialization list? 另外,我可以在初始化列表中为状态定义布尔值吗?

You've already declared that Information is an array of Pet objects. 您已经声明InformationPet对象的数组。 You don't need to declare it again for each element. 您无需为每个元素再次声明它。 Just remove Pet from that line. 只需从该行中删除“ Pet

Information[0] = {1, 4, 2016, 28, 3, "Male", false};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM