find the error

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

Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

find the error

 
0
  #1
Nov 18th, 2008
i cant seem to find what is wrong with this piece of code:
looks good to me-but my hw says it has an error in the code.
  1. struct TwoVals
  2. {
  3. int a = 5;
  4. int b = 10;
  5. };
  6.  
  7. int main()
  8. {
  9. TwoVals varray[1];
  10.  
  11. varray.a[0] =1;
  12. return0;
  13. }
Last edited by afg_91320; Nov 18th, 2008 at 2:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,805
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: find the error

 
1
  #2
Nov 18th, 2008
>my hw says it has an error in the code
I count four errors. Since you mention homework, is this a homework question that you should be doing instead of trying to get other people to answer?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

Re: find the error

 
0
  #3
Nov 18th, 2008
well i did try

i think its missing:

#include <iostream>
using namespace std;

but i dont see any other errors
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: find the error

 
0
  #4
Nov 18th, 2008
system("PAUSE");
return 0;
Last edited by NinjaLink; Nov 18th, 2008 at 2:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,805
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: find the error

 
1
  #5
Nov 18th, 2008
Originally Posted by afg_91320 View Post
well i did try

i think its missing:

#include <iostream>
using namespace std;

but i dont see any other errors
Originally Posted by NinjaLink View Post
system("PAUSE");
return 0;
That's just...wow. Okay afg, try compiling the code and see what your compiler thinks is wrong. You don't use anything from the iostream header or in namespace std, so including <iostream> and a using directive won't make a difference.

While NinjaLink probably fixed one of the errors by accident, you should still refrain from following his advice verbatim as it would only add more confusion to the mix by requiring a different header and opening you up to portability concerns.

And as a show of good faith, I'll point out two of the errors (which are actually caused by the same mistake):
struct TwoVals
{
      int a = 5;
      int b = 10;
};
You can't initialize structure members directly like that unless they meet certain requirements such as being static and const. The correct way is to set up a constructor with suitable defaults:
  1. struct TwoVals {
  2. int a;
  3. int b;
  4.  
  5. TwoVals ( int init_a = 5, int init_b = 10 )
  6. : a ( init_a ), b ( init_b )
  7. {}
  8. };
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 23
Reputation: stonerain is an unknown quantity at this point 
Solved Threads: 2
stonerain stonerain is offline Offline
Newbie Poster

Re: find the error

 
0
  #6
Nov 18th, 2008
  1. struct TwoVals
  2. {
  3. int a;
  4. int b;
  5. };
  6.  
  7. int main()
  8. {
  9. TwoVals varray[ 1 ];
  10.  
  11. varray[ 0 ].a = 1;
  12. }

Or you can do as Narue said.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,805
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: find the error

 
1
  #7
Nov 18th, 2008
stonerain, it's generally considered bad form to do people's homework for them. You'll notice that I went out of my way to encourage the OP to participate in answering the question, and you've ruined all of that work. Thanks.
I'm here to prove you wrong.
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC