954,193 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ beginner's question

;) 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;
}

;)

NavidV
Newbie Poster
5 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

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

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

NavidV

NavidV
Newbie Poster
5 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You