Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. accessing union inside struct

accessing union inside struct

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    M Agamawi
    wrote on last edited by
    #1

    Hi all I have the following scenario typedef struct _STRRET{ UINT uType; union { LPWSTR pol; UINT uof; char cStr[MAX_PATH]; }DUMMYUNIONNAME; }STRRET , *LPSTRRET; The problem is when i make a variable of type STRRET i couldn't acces the union variables. The following line include the code i wrote STRRET dispName; LPWSTR temp; temp = dispName.DUMMYUNIONNAME.pol; The compiler gives me compiling error: C2059 syntax error: '.' see declaration of _STRRET (which i know very good) Note: the above struct is a MFC defined type and don't know were is the problem may i have accessing the union in a wrong way or something. Sorry for my very stupid question and thanks for ur time in advance. Together we are stronger. I am so :wtf::((:confused::zzz: M.A.S.A

    _ 1 Reply Last reply
    0
    • M M Agamawi

      Hi all I have the following scenario typedef struct _STRRET{ UINT uType; union { LPWSTR pol; UINT uof; char cStr[MAX_PATH]; }DUMMYUNIONNAME; }STRRET , *LPSTRRET; The problem is when i make a variable of type STRRET i couldn't acces the union variables. The following line include the code i wrote STRRET dispName; LPWSTR temp; temp = dispName.DUMMYUNIONNAME.pol; The compiler gives me compiling error: C2059 syntax error: '.' see declaration of _STRRET (which i know very good) Note: the above struct is a MFC defined type and don't know were is the problem may i have accessing the union in a wrong way or something. Sorry for my very stupid question and thanks for ur time in advance. Together we are stronger. I am so :wtf::((:confused::zzz: M.A.S.A

      _ Offline
      _ Offline
      _Theo_
      wrote on last edited by
      #2

      Using another union name than DUMMYUNIONNAME solves the problem.

      M 1 Reply Last reply
      0
      • _ _Theo_

        Using another union name than DUMMYUNIONNAME solves the problem.

        M Offline
        M Offline
        M Agamawi
        wrote on last edited by
        #3

        I am sorry _Theo_ i don't understand u. The union name is defined in the MFC and i have no authority to midify it. So I must use the "DUMMYUNIONNAME " union name. Plllllllllllssssssssssss If u know any way of solving this problem i will be very greatful if u send it to me. Thank u Any way for ur quick reply and waiting for ur reply. I really want a solution for this problem. M.A.S.A

        _ 1 Reply Last reply
        0
        • M M Agamawi

          I am sorry _Theo_ i don't understand u. The union name is defined in the MFC and i have no authority to midify it. So I must use the "DUMMYUNIONNAME " union name. Plllllllllllssssssssssss If u know any way of solving this problem i will be very greatful if u send it to me. Thank u Any way for ur quick reply and waiting for ur reply. I really want a solution for this problem. M.A.S.A

          _ Offline
          _ Offline
          _Theo_
          wrote on last edited by
          #4

          Sorry I didn't realise it's a predefined structure. Leaving DUMMYUNIONNAME seems to work, so instead of using it call the union attributes right away like: //temp = dispName.DUMMYUNIONNAME.pol; temp = dispName.pol;

          A 1 Reply Last reply
          0
          • _ _Theo_

            Sorry I didn't realise it's a predefined structure. Leaving DUMMYUNIONNAME seems to work, so instead of using it call the union attributes right away like: //temp = dispName.DUMMYUNIONNAME.pol; temp = dispName.pol;

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            _Theo_ thank u veeeeeeeeeeeeeeeeeeeeryyyyyyyyyyyy muchhhhhhhhhhhhhhhhhhhhhhh I don't know how to thank u for ur advice. U will not belive me that i have tried it before and it didn't work but after reading ur message i tried it again but this time it works. This job will make me made especially this stupid mistakes. Thank u again.

            J 1 Reply Last reply
            0
            • A Anonymous

              _Theo_ thank u veeeeeeeeeeeeeeeeeeeeryyyyyyyyyyyy muchhhhhhhhhhhhhhhhhhhhhhh I don't know how to thank u for ur advice. U will not belive me that i have tried it before and it didn't work but after reading ur message i tried it again but this time it works. This job will make me made especially this stupid mistakes. Thank u again.

              J Offline
              J Offline
              jbarton
              wrote on last edited by
              #6

              Just to clarify why this works: Microsoft uses a #define for DUMMYUNIONNAME so that the same include file can be used with compilers that don't support unnamed unions. When you compile using Visual C++, the DUMMYUNIONNAME is #defined to a blank string, so the union doesn't have a name (and its members can be accessed directly). When you compile on a compiler which doesn't support unnamed unions, it is #defined to u (and you would need to include the .u before the member name).

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups