944,183 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1253
  • C++ RSS
Jul 31st, 2006
0

C++ beginner's question

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. struct CandyBar
  6. {
  7. string brand;
  8. int calories;
  9. float weight;
  10. };
  11. CandyBar *snack = new CandyBar[3];
  12.  
  13. snack[0] = {"Milky Way", 350, 2.3}; //**Error before { token
  14. snack[1] = {"Hershey's", 400, 3.3};
  15. snack[2] = {"Dove", 450, 4.3};
  16.  
  17. delete [] snack;
  18.  
  19. return 0;
  20. }
Last edited by Dave Sinkula; Jul 31st, 2006 at 11:58 pm. Reason: Added [code][/code] tags -- learn to use them yourself.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NavidV is offline Offline
5 posts
since Jul 2006
Aug 1st, 2006
0

Re: C++ beginner's question

You can initialize elements like that, but you are trying to assign them, which you cannot do. You can assign each member individually.
Last edited by Dave Sinkula; Aug 1st, 2006 at 12:11 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 1st, 2006
0

Re: C++ beginner's question

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

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

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help With Sftp In C++
Next Thread in C++ Forum Timeline: Console setting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC