在一些资料上看到,如果struct里有构造函数,它将不能作为union里的成员。而我的struct里压根就没有函数,可是放在union里时,还是报错。不知道怎么回事。请大家指教~!

解决方案 »

  1.   

    另外, 联合既可以出现在结构内, 它的成员也可以是结构。  
        例如:  
         struct{  
              int age;  
              char *addr;  
              union{  
                   int i;  
                   char *ch;  
              }x;  
         }y[10];  
        若要访问结构变量y[1]中联合x的成员i, 可以写成:  
          y[1].x.i;  
        若要访问结构变量y[2]中联合x的字符串指针ch的第一个字符可写成:  
          *y[2].x.ch;  
        若写成"y[2].x.*ch;"是错误的。
      

  2.   

    typedef struct _rcs_cmd_buf_stru
    {
        UINT8 status;
        UINT8 type;
        UINT8 len;
        cmd_process fun;
        union
        {
            UINT8 buf[RCS_RWBUF_LEN];
            rcs_attention_stru atten_buf;
            rcs_authen_stru authen_buf;
            rcs_rw_cmd_stru rw_c_buf;
            rcs_card_uecmd_stru card_uec_buf;
            rcs_card_ecmd_stru card_ec_buf;
        }buffer;
    }rcs_cmd_buf_stru, *rcs_cmd_buf_stru_ptr;
    怎么可以?
      

  3.   

    怎么都是union做struct的成员?我问的是struct做union的成员呀~!struct abc
    {
       int a;
       long b;
       //...
       union {
          struct example ex;
          struct example2 ex2;
       } my_ex;
    }结果就报错了。
      

  4.   

    我找到原因了。由于结构太复杂,一时没发现,我的union里最终包含一个POSITION变量,估计是它的问题了。谢谢大家。