以下是我Character
类的构造函数:
Character(int, int, int, string, string, string, string, string, double, double, bool, bool);
在主要功能中,这是我尝试的方式:
Character character[100];
character[0] = {777, 10, 5, "one", "type", "desc", "status", "knife", 10.0, 20.0, false, true};
character[1] = {707, 5, 10, "two", "type2", "desc2", "status", "knife", 15.0, 23.4, true, true};
错误状态,它没有存储类或类型说明符..
编辑* 最后用这个做对了: p / s - 请提供更好的解决方案。 = /
Character character[100] = { character[0] = { 777, 10, 5, "one", "type", "desc", "status", "knife", 10.0, 20.0, false, true },
character[1] = { 707, 5, 10, "two", "type2", "desc2", "status", "gun", 15.0, 23.4, true, true },
character[2] = { 888, 7, 43, "three", "type3", "desc3", "status", "sword", 15.0, 23.4, false, false } };
答案 0 :(得分:2)
假设您已正确定义operator=
,您可以执行以下操作:
Character character[100];
character[0] = Character( 777, 10, 5, "one", "type", "desc", "status", "knife", 10.0, 20.0, false, true );
character[1] = Character( 707, 5, 10, "two", "type2", "desc2", "status", "knife", 15.0, 23.4, true, true );
答案 1 :(得分:1)
如果你只想有选择地初始化数组中的元素,C99
有designated initializers,它似乎也适用于C ++。
Character character[100] = {
[0] = Character( 777, 10, 5, "one", "type", "desc", "status", "knife", 10.0, 20.0, false, true ),
[1] = Character( 707, 5, 10, "two", "type2", "desc2", "status", "knife", 15.0, 23.4, true, true )
};
您还必须为其余元素设置默认的Character
构造函数。
然而,在C ++ 11中这个功能seems to not exist但gcc似乎可以处理它(可能通过扩展)。