简体   繁体   English

转换为枚举类型需要显式强制转换(static_cast,C样式强制转换或函数样式强制转换)枚举

[英]Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast) enum

I have copied the variable data from enum type which is declared outside the structure to the enum type variable which is written inside structure but it is throwing building error as Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast) 我已经将变量数据从在结构外部声明的枚举类型复制到在结构内部写入的枚举类型变量,但由于引发向枚举类型的转换需要显式强制转换(static_cast,C样式强制转换或函数-样式转换)

enum TGenObjType                        //sundar
{
   TYPE_UNDEFINED 
   , TYPE_CAR      
    , TYPE_TRUCK     
    , TYPE_MOTORBIKE 
   , TYPE_BIKE      
  , TYPE_PEDESTRIAN
  };

struct SGenObjData
{
  enum TGenObjType                recogType                          ; /* #RTAS: input               */ /* Object type (vehicle, pedestrian, ...)                                      */
  enum TGenObjType                commuType                          ; /* #RTAS: input               */ /* Object type (vehicle, pedestrian, ...)                                      */
}; 


 ushort j(0);
   for (ulong i = 0; i < gol.mData.size(); ++i)
 {
   if (gol.mData[i].isValid > 0)
    {
     if (gol.mData[i].id <= OBJ_MAX_ID)
     {
        objectList.objectIdx[size_t(gol.mData[i].id)][size_t(SRC_LIST1)]           = ushort(j);

             switch (gol.mData[i].recognized_shape)
            {
            case EO_SHAPE_CAR       : objectList.objects[j].recogType =    TYPE_CAR           ; break;
            case EO_SHAPE_TRUCK     : objectList.objects[j].recogType = TYPE_TRUCK         ; break;
            case EO_SHAPE_MOTORBIKE : objectList.objects[j].recogType = TYPE_MOTORBIKE ; break;
           case EO_SHAPE_BIKE      : objectList.objects[j].recogType = TYPE_BIKE      ; break;
           case EO_SHAPE_PEDESTRIAN: objectList.objects[j].recogType = TYPE_PEDESTRIAN; break;
            case EO_SHAPE_UNDEFINED :                                                                  
            default                 :    myDataLst.objectList.objects[j].recogType =    TYPE_UNDEFINED ;
         }// throwing an error Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)

can anyone tell me what went wrong? 谁能告诉我哪里出了问题?

A named enum is a distinct type. 命名enum是一种独特的类型。 it has an underlying type and a "weak" enum allows implicit conversion to the underlying type. 它具有基础类型,“弱” enum允许隐式转换基础类型。 It does not support implicit conversion from the underlying type to the enum . 它不支持从基础类型到enum隐式转换。 An enum declared with the same name as another enum but in a different scopr is a distinct type of its own (this applies to all types). 与另一个enum声明相同名称但在另一个scopr中声明的enum是其自身的独特类型(这适用于所有类型)。 Since the enum does not support implicitly conversion from the underlying type you cannot simply assign the value of one enum to another, even if the enum 's and all of their members have identical names and values. 由于enum不支持基础类型隐式转换,因此即使enum及其所有成员具有相同的名称和值,也不能简单地将一个enum的值分配给另一个enum

enum Foo
{
    Bar = 0
};

is not the same as 与...不同

struct Object
{
    enum Foo
    {
        Bar = 0
    };
};

Both Foo and Object::Foo are distinct types and C++ does not allow implicitly conversion between them. FooObject::Foo都是不同的类型,C ++不允许在它们之间进行隐式转换。

Object:Foo innerFoo = Bar; // Error Bar and Object::Bar are not the same

Object:Foo innerFoo = Onject::Bar; // OK! They are the same!

It sounds like the two enum 's are redundant so you need to decide which version of TGenObjType you want to use and delete the other. 听起来这两个enum是多余的,所以您需要确定要使用的TGenObjType版本,并删除另一个版本。

暂无
暂无

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

相关问题 转换为枚举类型需要显式强制转换(static_cast,C样式强制转换或函数样式强制转换) - Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast) C ++强制转换显式类型转换(C样式转换)和static_cast的多种解释 - C++ cast notation of explicit type conversion (C-style cast) and multiple interpretations of static_cast 转换需要reinterpret_cast,C样式转换或函数样式转换 - Conversion requires reinterpret_cast, C-style cast or function-style cast 错误:从整数类型到指针类型的转换需要reinterpret_cast,C样式强制转换或函数样式强制转换 - Error: Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast C风格的演员是否与功能风格的演员阵容相同? - Is a C-style cast identical to a function-style cast? static_cast引发错误,但C样式强制转换有效 - static_cast throws error but C-style cast works 将C风格的演员表改为static_cast总是安全的吗? - Is it always safe to change a C-style cast to a static_cast? 为什么这种 C 风格的转换不考虑 static_cast 后跟 const_cast? - Why does this C-style cast not consider static_cast followed by const_cast? 使用C ++样式转换从Void *转换为TYPE *:static_cast或reinterpret_cast - Cast from Void* to TYPE* using C++ style cast: static_cast or reinterpret_cast 函数式强制转换或类型构造需要 &#39;(&#39; - Expected '(' for function-style cast or type construction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM