【发布时间】:2017-09-11 04:12:34
【问题描述】:
我正在使用 C++ 11/14 (Visual Studio 2015) 构建解析器。如何在类中创建和初始化表,并让它计算表的长度?
我从编译器收到以下错误:
1>d:\temp\win32project1\win32project1\source1.cpp(49):警告 C4200:使用了非标准扩展:结构/联合中的零大小数组
1> d:\temp\win32project1\win32project1\source1.cpp(49):注意:该成员将被默认构造函数或复制/移动赋值运算符忽略
1>d:\temp\win32project1\win32project1\source1.cpp(49): error C2997: 'tlv::_requests': array bound cannot be deduced from an in-class initializer
1> d:\temp\win32project1\win32project1\source1.cpp(45): 注意:见 'tlv::_requests' 的声明
typedef enum _TLV_TYPE : UINT32
{
tlv_type_read,
tlv_type_write,
tlv_type_param,
tlv_type_status
} TLV_TYPE;
typedef struct
{
TLV_TYPE type;
UINT32 length;
} TLV_RECORD, *pTLV_RECORD;
typedef std::function <UINT32 (pTLV_RECORD Record)> pTLV_PARSER;
typedef struct
{
TLV_TYPE type;
pTLV_PARSER parse_routine;
vector <TLV_RECORD> parameters;
vector <TLV_RECORD> response;
} TABLE, *pTABLE;
class tlv
{
public:
tlv (SOCKET Socket);
~tlv ();
UINT32 start_parse (pTLV_RECORD Record);
protected:
pTLV_PARSER parse_read;
pTLV_PARSER parse_write;
const TABLE _requests [] =
{
{tlv_type_read, parse_read, {{tlv_type_param, 4}, {tlv_type_param, 0}, {tlv_type_param, 4}}, {{tlv_type_status, 4}}},
{tlv_type_write, parse_write,{{tlv_type_param, 4}}, {{tlv_type_status, 4}}},
};
}; // End of class tlv
【问题讨论】:
-
离题:大部分
typdefing 在 C++ 中是不需要的。 -
typedef struct/enum _foo { ... } foo;是 C++ 中的反模式。编写定义的正确方法是enum TLV_TYPE : UINT32 { ... };。你正在做的typedef废话用在 C 中,但不是 C++。 -
此代码compiles fine for me。如果您遇到错误,请将它们包含在您的问题中。
-
@cdhowie,我收到以下错误:警告 C4200:使用了非标准扩展:结构/联合中的零大小数组注意:此成员将被默认构造函数或复制/移动赋值运算符错误忽略C2997: 'tlv_::_requests': 数组绑定不能从类内初始化器中推导出注:参见 'tlv_::_requests' 错误声明 C2229: 类 'tlv_' 有一个非法的零大小数组
标签: c++