Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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++ Flatbuffers创建枚举向量_C++_Flatbuffers - Fatal编程技术网

C++ Flatbuffers创建枚举向量

C++ Flatbuffers创建枚举向量,c++,flatbuffers,C++,Flatbuffers,我在创建枚举向量时遇到问题。 即,给定以下模式文件: enum Month : short { January, February, March, } table SpecificMonths { Months:[Month]; } root_type SpecificMonths; 我正在尝试创建SpecificMonths类型的对象: flatbuffers::FlatBufferBuilder builder; Month months[] = {Mo

我在创建枚举向量时遇到问题。 即,给定以下模式文件:

enum Month : short
{
    January,
    February,
    March,
}

table SpecificMonths
{
    Months:[Month];
}

root_type SpecificMonths;
我正在尝试创建SpecificMonths类型的对象:

flatbuffers::FlatBufferBuilder builder;
Month months[] = {Month_January, Month_March};
auto monthsVector = builder.CreateVector(months, 2);
auto specificMonths = CreateSpecificMonths(builder, monthsVector);
但这会带来错误:

error C2664: 'flatbuffers::Offset<SpecificMonths> CreateSpecificMonths(flatbuffers::FlatBufferBuilder &,flatbuffers::Offset<flatbuffers::Vector<int16_t>>)': cannot convert argument 2 from 'flatbuffers::Offset<flatbuffers::Vector<Month>>' to 'flatbuffers::Offset<flatbuffers::Vector<int16_t>>'

note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
错误C2664:'flatbuffers::Offset CreateSpecificMonths(flatbuffers::FlatBufferBuilder&,flatbuffers::Offset)':无法将参数2从'flatbuffers::Offset'转换为'flatbuffers::Offset'
注意:没有可执行此转换的用户定义的转换运算符,或者无法调用该运算符

有办法吗?我更喜欢没有STD的::vector(不分配)。< /P> < P>问题是默认情况下,C++枚举没有指定的基础类型,尽管它通常默认为“代码> int <代码>。因此,您的
months
数组不能传递给
CreateVector
,即使您铸造了它,因为它们具有不同的大小元素

最简单的解决方案可能是将其声明为
int16\u t months[]
,您的编译器应该可以将每个枚举值转换为初始值设定项,而无需强制转换

或者,使用
--作用域枚举编译模式,以获得具有适当底层类型的C++11样式枚举。我猜在这种情况下,编译器仍然会抱怨将
months
传递给
CreateVector
,因为现在枚举是强类型的,但至少现在您可以安全地强制转换它,因为值现在大小相同(例如
reinterpret\u cast(months)