I'm trying to initialize the members of candy like this.
#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;
}
But it gives me a syntax error near the opening brace i know this is wrong but is there a way like this to initialize a array of struct. And can someone explain why is the above code wrong.
std::stringas a member unless there was a constructor to call, but it does on GCC at least :)5.17assignment, but what I think happens is that copy assignment: semantically, the RHS initializar list converts to aCandyBartemporary, which is then used for assignment.