简体   繁体   English

将unsigned int分配给结构中的数组枚举时的编译错误

[英]Compilation error in assigning unsigned int to an enum of arrays within structure

typedef struct {
uint64_t low;
uint64_t cache_size;
uint32_t range;
uint8_t cache;

/// Number of symbols in the tables
size_t count;

/// rc_encode()'s position in the tables
size_t pos;

/// Symbols to encode
enum {
    RC_BIT_0,
    RC_BIT_1,
    RC_DIRECT_0,
    RC_DIRECT_1,
    RC_FLUSH,
} symbols[RC_SYMBOLS_MAX];

/// Probabilities associated with RC_BIT_0 or RC_BIT_1
probability *probs[RC_SYMBOLS_MAX];

} lzma_range_encoder;

//Above is the structure. //上面是结构。

//Below is the function //下面是函数

static inline void
rc_bit(lzma_range_encoder *rc, probability *prob, uint32_t bit)
{
rc->symbols[rc->count] = bit;      // problem code line 69
rc->probs[rc->count] = prob;
++rc->count;
}

//Error: error C2440: '=' : cannot convert from 'uint32_t' to 'lzma_range_encoder::' Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast) //错误:错误C2440:'=':无法从'uint32_t'转换为'lzma_range_encoder ::'转换为枚举类型需要显式转换(static_cast,C样式转换或函数样式转换)

'bit' is uint32_t which needs to be stored(type-casted) in lzma_range_encoder->symbols, but I am not able to do it somehow. 'bit'是uint32_t,需要在lzma_range_encoder->符号中存储(类型化),但我无法以某种方式进行。 tried every p&c. 尝试了所有的p&c。 Also, searched earlier questions regarding this(static_cast and all but no luck) 此外,搜索有关此问题的早期问题(static_cast和所有但没有运气)

It is a simple problem I guess. 我想这是一个简单的问题。 But I am stuck since 2 days. 但是我被困2天了。 Please help. 请帮忙。 Thanks 谢谢

Add a name to the enumeration: 在枚举中添加一个名称

enum symbol_enum
{
    // ...
} symbols[RC_SYMBOLS_MAX];

Then you can cast to it: 然后你可以投射它:

rc->symbols[rc->count] = static_cast<lzma_range_encoder::symbol_enum>(bit);

PS. PS。 In C++ you don't need to use typedef for structures or classes. 在C ++中,您不需要对结构或类使用typedef The normal structure name can be used as-is. 普通结构名称可以按原样使用。

You can use following thing with unnamed enum with C++11. 你可以使用C ++ 11中带有未命​​名枚举的以下内容。

enum {
    RC_BIT_0,
    RC_BIT_1,
    RC_DIRECT_0,
    RC_DIRECT_1,
    RC_FLUSH,
} symbols[RC_SYMBOLS_MAX];

rc->symbols[rc->count] = 
static_cast<std::decay<std::decltype(*symbols)>::type>(bit);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM