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;现在可以了,谢谢。