C++ beginner's question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 5
Reputation: NavidV is an unknown quantity at this point 
Solved Threads: 0
NavidV's Avatar
NavidV NavidV is offline Offline
Newbie Poster

C++ beginner's question

 
0
  #1
Jul 31st, 2006
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,361
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C++ beginner's question

 
0
  #2
Aug 1st, 2006
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 5
Reputation: NavidV is an unknown quantity at this point 
Solved Threads: 0
NavidV's Avatar
NavidV NavidV is offline Offline
Newbie Poster

Re: C++ beginner's question

 
0
  #3
Aug 1st, 2006
Thanks for your suggestion Dave. It worked. I'm a happy camper now.

NavidV
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC