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

Recommended Answers

All 6 Replies

>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?

well i did try

i think its missing:

#include <iostream>
using namespace std;

but i dont see any other errors :(

system("PAUSE");
return 0;

well i did try

i think its missing:

#include <iostream>
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 <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:

struct TwoVals {
  int a;
  int b;

  TwoVals ( int init_a = 5, int init_b = 10 )
    : a ( init_a ), b ( init_b )
  {}
};
struct TwoVals
{
      int a;
      int b;
};

int main()
{
    TwoVals varray[ 1 ];

     varray[ 0 ].a = 1;   
}

Or you can do as Narue said.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.