Link to home
Start Free TrialLog in
Avatar of johnthomaswarner
johnthomaswarner

asked on

C language in Visual studio Struct within a struct: error CXX0025: Error:operator needs class/struct/union

Hopefully someone can tell me what I'm doing wrong.
I have a struct that contains a struct. When I try to access properties of the struct contained
within the first struct, I receive an error:
 CXX0025: Error:operator needs class/struct/union

From what I can tell this is proper syntax in C; however, there must be something specific
to the Microsoft C++ compiler that I'm not getting.
I created a win32 project in visual studio 2008.

Thanks.

void testStructConcept()
{      
      struct first_structure_type
      {
            int integer_member;
            float float_member;
      };
 
      struct second_structure_type
      {
            double double_member;
            struct first_structure_type struct_member;
    };

    struct second_structure_type demo;
 
      demo.double_member = 12345.6789;
      demo.struct_member.integer_member = 5;
      demo.struct_member.float_member = 1023.17;
}
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Change your code as follows...

NB. Although you can define structs in a function the semantics are somewhat different to defining them outside a function (the normal place for you to put them).
      struct first_structure_type
      {
            int integer_member;
            float float_member;
      };
  
      struct second_structure_type
      {
            double double_member;
            struct first_structure_type struct_member;
    };


void testStructConcept()
{      

    struct second_structure_type demo;
  
      demo.double_member = 12345.6789;
      demo.struct_member.integer_member = 5;
      demo.struct_member.float_member = 1023.17;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of johnthomaswarner
johnthomaswarner

ASKER

It worked! Is this specific to the Visual C++ compiler or is this a C thing?
Very fast response!
Well, the specifics for using local classes/structs are part of the C++ standard; however, VS2008 is a little bit loose with this. In this case, your original code should have compiled (and does in gcc) but my experience of Visual Studio is that it doesn't play well with local classes/structs and the reasons for using them are rarely enough to justify the confusion they cause to others or the compiler.