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

Dani AI

Generated

Quick summary for : the snippet actually has four distinct problems (two of them are the same kind). These are the two illegal in-class initializers for non-static members, the wrong order of array/member access, and the malformed return statement. The rest of the thread already points to the fixes, but a few clarifications and portable alternatives will help future readers.

  1. Member initialization: the lines that try to write defaults inside the struct are not valid under the C++ standard in effect around 2008 (C++03). That is why the compiler will complain. Two portable fixes are (a) remove those initializers and set defaults in a constructor, or (b) initialize the object when declared using aggregate/braced initialization. Modern compilers with C++11+ accept in-class initializers, so switching to a C++11-or-later mode also resolves it.

  2. Array/member access: varray.a[0] is invalid because the array must be indexed first, then a member accessed (index the array element, then access its a member). Pointer notation (element pointer -> member) is the same idea. This is what illustrated.

  3. Syntax: return0; is a token error — it must be return 0;.

Extras: #include <iostream> / using namespace std; are unnecessary unless i/o is used. As warned, system("PAUSE") (suggested by ) is non‑portable and generally best avoided for homework or portable code. Compiler error messages give the exact lines to fix; reading them will show these exact problems.

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.