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;
}
Visual C++.NETC

Avatar of undefined
Last Comment
evilrix

8/22/2022 - Mon
evilrix

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
evilrix

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
johnthomaswarner

ASKER
It worked! Is this specific to the Visual C++ compiler or is this a C thing?
johnthomaswarner

ASKER
Very fast response!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
evilrix

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.