;) Hello,

I have just started to learning C++. I have a question regarding a small code snippet that I am having trouble with. I get a compiler error saying that there is a parse error before the { token. Can anyone help me with this?

Thanks,
Navid

Here's the code:

#include <iostream>
using namespace std;

int main() {
 struct CandyBar 
    {
        string brand;
        int calories;
        float weight;
    };
    CandyBar *snack = new CandyBar[3];        
    
        snack[0] = {"Milky Way", 350, 2.3};  //**Error before { token
        snack[1] = {"Hershey's", 400, 3.3};
        snack[2] = {"Dove", 450, 4.3};

   delete [] snack;
        
    return 0;
}

;)

You can initialize elements like that, but you are trying to assign them, which you cannot do. You can assign each member individually.

Thanks for your suggestion Dave. It worked. I'm a happy camper now.

NavidV

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.