简体   繁体   English

枚举类型错误C2677

[英]Enum type error C2677

my problem is, i got this enum type declared in my Button.h file: 我的问题是,我在Button.h文件中声明了此枚举类型:

enum ButtonEnum
    {
        BUTTON_SPRITE_MOUSE_UP_RED = 0,
        BUTTON_SPRITE_MOUSE_UP_VIOLET = 1,
        BUTTON_SPRITE_MOUSE_UP_DBLUE = 2,
        BUTTON_SPRITE_MOUSE_UP_GREY = 3,
        BUTTON_SPRITE_MOUSE_UP_DGREEN = 4,
        BUTTON_SPRITE_MOUSE_UP_LBLUE = 5,
        BUTTON_SPRITE_MOUSE_UP_LGREEN = 6,
        BUTTON_SPRITE_MOUSE_UP_YELLOW = 7,
        BUTTON_SPRITE_MOUSE_UP_EMPTY = 8,
        BUTTON_SPRITE_TOTAL = 9
    };

Later, i declare this: 后来,我宣布:

ButtonEnum mCurrentSprite;

and use it in other function in Button.cpp: 并在Button.cpp的其他函数中使用它:

void Button::render()
{
    SDLFunctions::render(mPosition.x,mPosition.y,SDLFunctions::gSpriteClips[mCurrentSprite] );
}

SDLFunctions::render looks like that: SDLFunctions :: render看起来像这样:

void SDLFunctions::render( int x, int y, SDL_Rect* clip )
{
    //Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, mWidth, mHeight };

//Set clip rendering dimensions
if( clip != NULL )
{
    renderQuad.w = clip->w;
    renderQuad.h = clip->h;
}

//Render to screen
SDL_RenderCopy( gRenderer, mTexture, clip, &renderQuad );
}

and SDLFunctions::gSpriteClips is this: 和SDLFunctions :: gSpriteClips是这样的:

SDL_Rect gSpriteClips[ 9 ];

which in another function is filled with some values. 它在另一个函数中填充了一些值。 To the point. 要点。 Building program results with this: error C2677: binary '[' : no global operator found which takes type 'Button::ButtonEnum' (or there is no acceptable conversion) 使用以下命令生成程序结果:错误C2677:二进制'[':未找到采用类型'Button :: ButtonEnum'的全局运算符(或没有可接受的转换)

And my question is: what the hell? 我的问题是:到底是什么? What is wrong with this? 这有什么问题? mCurrentSprite got declared starting value in builder, and everytime Button::render is called, it got some value, its not NULL. mCurrentSprite在生成器中声明了起始值,并且每次调用Button :: render时,它都有一些值,而不是NULL。 Can someone solve it? 有人可以解决吗? Thanks. 谢谢。

Your compiler is trying to tell you (in a very unclear way) that you are indexing into an array using an enum. 您的编译器试图(以一种非常不清楚的方式)告诉您您正在使用枚举索引到数组中。 That's not really allowed--enums have integral values, but they aren't integer types. 确实不允许这样做-枚举具有整数值,但它们不是整数类型。 So you need to cast it: 因此,您需要强制转换:

SDLFunctions::render(mPosition.x, mPosition.y,
    SDLFunctions::gSpriteClips[static_cast<size_t>(mCurrentSprite)]);

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

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