我试图初始化这样的糖果的成员。结构数组将不会初始化
#include <iostream>
#include <string>
struct CandyBar
{
std::string Brand;
float weight;
int cal;
};
int main()
{
CandyBar candy[3];
candy[0] = {"toe foe", 30.2f, 500};
candy[1] = {"lays", 2.1f, 10};
candy[2] = {"fin", 40.5f, 1000};
return 0;
}
但它给了我附近的开括号 我知道这是错误的,但有没有这样一种方法来初始化结构的一个阵列中的语法错误。 有人可以解释为什么是上述代码错误。
任何理由没有构造? – chris
您的代码在C++ 11中按原样工作。 – bames53
@ bames53,Huh,不认为可以使用'std :: string'作为成员,除非有一个构造函数要调用,但它至少在GCC上执行:) – chris