typedef结构默认初始化

活泼

如何对结构进行默认初始化?

我正在尝试为我的结构进行默认初始化,但出现此错误:

error: request for member 'type' in '((CCatManager::cat_shop_item*)this)->CCatManager::cat_shop_item::catAttr', which is of non-class type 'TCategoryAttribute [0]'
..........................

这是我的代码:

enum EMisc
{
    CAT_MAX_NUM = 8,
};

typedef struct TCategoryAttribute
{
    BYTE    type;
    short   value;
} TCategoryAttribute;

typedef struct category_items
{
    long    price;
    DWORD   order;

    TCategoryAttribute    catAttr[CAT_MAX_NUM];

    category_items()
    {
        price = 0;
        order = 0;

        catAttr.type[0] = 0;
        .....
        catAttr.type[7] = 0;

        catAttr.value[0] = 0;
        .....
        catAttr.value[7] = 0;
    }
} CATEGORY_ITEMS;

“价格”和“订单”都可以,但是可以使用,但是TCategoryAttribute不起作用...

我真的很困惑……在此先感谢,我希望这个问题是正确的。


解决了,多亏@Michael。

怀念

那是因为type不是数组,而是数组catAttr其次,您声明了其中的3个,因此您的第三个项目位于索引2。请记住,您从0开始计数。因此将代码的这一部分更改为:

category_items()
    {
        price = 0;
        order = 0;

        catAttr[0].type = 0;
        catAttr[1].type = 0;
        catAttr[2].type = 0;
        //catAttr[3].type = 0; //you can't hit [3]

        catAttr[0].value = 0;
        catAttr[1].value = 0;
        catAttr[2].value = 0;
        //catAttr.value[3] = 0;
    }

在此处查看可编译(且可运行)的完整代码:http : //coliru.stacked-crooked.com/a/abda47415ebb9938

顺便说一句,那typedef是多余的。只是做这个...

struct TCategoryAttribute
{
    BYTE    type;
    short   value;
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章