Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ typedef结构默认初始化_C++_Struct_Typedef - Fatal编程技术网

C++ typedef结构默认初始化

C++ typedef结构默认初始化,c++,struct,typedef,C++,Struct,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 EMis

如何为结构进行默认初始化

我正在尝试对结构进行默认初始化,但出现以下错误:

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;
    }
请参见此处编译(并运行)的完整代码:

顺便说一句,
typedef
是多余的。就这么做吧

struct TCategoryAttribute
{
    BYTE    type;
    short   value;
};

你是指<代码> CATTAFR(0)。type =…< /Cuff>??BTW,你应该用的最大长度为3的数组是2。你在C还是C++?C是一种不同的语言!不要为C++问题添加C标记(反之亦然)。很抱歉,我将删除它。您使用的编译器是什么?您的编译器标志是什么?catAttr.type[0]=0;->catAttr[0]。类型=0;现在可以了,谢谢。