4

I use open-source code that declared static union inside class like this.

VAD.h:

class VD
{   
public:
    static union Wu
    {
        const short w[2 * 64];
        const double y[16];      
    } wu; 
};

VAD.cpp:

VD:: Wu  VD:: wu =
{
     0x0000,  0x7FFF,  0x0000,  0x7FFF,  0x0000,  0x7FFF,  0x0C8B,  0x7F61,
     0x18F8,  0x7D89,  0x2527,  0x7A7C,  0x18F8,  0x7D89,  0x30FB,  0x7641,
     0x471C,  0x6A6D,  0x2527,  0x7A7C,  0x471C,  0x6A6D,  0x62F1,  0x5133,
     0x30FB,  0x7641,  0x5A82,  0x5A82,  0x7641,  0x30FB,  0x3C56,  0x70E2,
     0x6A6D,  0x471C,  0x7F61,  0x0C8B,  0x471C,  0x6A6D,  0x7641,  0x30FB,
     0x7D89, -0x18F8,  0x5133,  0x62F1,  0x7D89,  0x18F8,  0x70E2, -0x3C56,
     0x5A82,  0x5A82,  0x7FFF,  0x0000,  0x5A82, -0x5A82,  0x62F1,  0x5133,
     0x7D89, -0x18F8,  0x3C56, -0x70E2,  0x6A6D,  0x471C,  0x7641, -0x30FB,
     0x18F8, -0x7D89,  0x70E2,  0x3C56,  0x6A6D, -0x471C, -0x0C8B, -0x7F61,
     0x7641,  0x30FB,  0x5A82, -0x5A82, -0x30FB, -0x7641,  0x7A7C,  0x2527,
     0x471C, -0x6A6D, -0x5133, -0x62F1,  0x7D89,  0x18F8,  0x30FB, -0x7641,
    -0x6A6D, -0x471C,  0x7F61,  0x0C8B,  0x18F8, -0x7D89, -0x7A7C, -0x2527,
     0x0000,  0x7FFF,  0x0000,  0x7FFF,  0x0000,  0x7FFF,  0x30FB,  0x7641,
     0x5A82,  0x5A82,  0x7641,  0x30FB,  0x5A82,  0x5A82,  0x7FFF,  0x0000,
     0x5A82, -0x5A82,  0x7641,  0x30FB,  0x5A82, -0x5A82, -0x30FB, -0x7641
};

when I compiled this code using VC++ 6.0 on windows..this error occured:

public: static union VD::Wu VD::wu' : non-aggregates cannot be initialized with initializer list

anyone can help me please?

jww
  • 97,681
  • 90
  • 411
  • 885
NMMA
  • 173
  • 1
  • 2
  • 13
  • Related, that probably leads to undefined behavior in C++ (but not C). We can't say for certain because we need to see how `Wu` is used. Also see [Accessing inactive union member and undefined behavior?](https://stackoverflow.com/q/11373203/608639) – jww Mar 31 '18 at 01:08

1 Answers1

4

You need an extra pair of curly braces, as you want to initialize the array that is nested inside the union:

VD:: Wu  VD:: wu =
{
  { // <-- forgot
    <snip />
  } // <-- these
};
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • @user: Strange, both versions work for me on VS2010, so I think your Visual Studio is just way too old. :| – Xeo Jun 20 '11 at 07:32