find the error
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.
struct TwoVals
{
int a = 5;
int b = 10;
};
int main()
{
TwoVals varray[1];
varray.a[0] =1;
return0;
}
afg_91320
Junior Poster in Training
55 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
>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?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
well i did try
i think its missing:
#include
using namespace std;
but i dont see any other errors :(
afg_91320
Junior Poster in Training
55 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
well i did try
i think its missing:
#include
using namespace std;
but i dont see any other errors :(
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 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:
struct TwoVals {
int a;
int b;
TwoVals ( int init_a = 5, int init_b = 10 )
: a ( init_a ), b ( init_b )
{}
};
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401