不一致的 C26496 警告

Inconsistent C26496 warning

我最近使用结构重构了我的 SDL2 C++ 代码。它编译并运行良好(使用 Visual Studio 2019),但现在抛出一个不一致的警告:

C26496 变量 'Graphic::X' 未初始化。始终初始化一个成员变量。

这对于表示 renderTexture 函数的代码行重复 X = h、w、x 和 y。但是,之前出现的 mouseOverRect 函数不会出现警告,这两个函数都以完全相同的方式使用 Graphic 结构变量。

我在下面包含了我的代码的相关部分。我想弄清楚是什么原因导致了警告以及为什么它没有对这两个函数重复?

头文件

    int mouse_x;
    int mouse_y;

    struct Graphic
    {
        std::string name;
        std::string type;
        int x;
        int y;
        int h;
        int w;
    };

源文件

    //Initialization
    SDL_Window* window = NULL;                                      
    SDL_Renderer* renderer = NULL;
    SDL_Rect* clip = NULL;

    //Log Error Function
    void logSDLError(std::ostream &os, const std::string &msg)
    {
        os << msg << " error: " << SDL_GetError() << std::endl;
    }

    //Mouse Over Rectangle Function
    bool mouseOverRect(int x, int y, Graphic &graphic)
    {
        if ((x <= graphic.x + graphic.w) && (x > graphic.x) &&
            (y <= graphic.y + graphic.h) && (y > graphic.y))
            return true;
        else
            return false;

    //Render Texture at Destination Function
    void renderTexture(SDL_Texture* texture, SDL_Renderer* renderer, Graphic &graphic,
        SDL_Rect *clip = nullptr)
    {
        SDL_Rect dest;
        dest.x = graphic.x;
        dest.y = graphic.y;
        dest.h = graphic.h;
        dest.w = graphic.w;

        SDL_RenderCopy(renderer, texture, clip, &dest);
    }

    // Main Function
    int main(int, char**)
    {
        //Start up SDL
        if (SDL_Init(SDL_INIT_VIDEO) != 0)
        {
            logSDLError(std::cout, "Cannot initialize SDL video.");
        }

        //Setup window and renderer
        SDL_Window *window = SDL_CreateWindow("Dragon Hunt", SDL_WINDOWPOS_CENTERED,
                SDL_WINDOWPOS_CENTERED, Screen_Width, Screen_Height, SDL_WINDOW_RESIZABLE);
        if (window == nullptr)
        {
            logSDLError(std::cout, "Cannot create window.");
        }
        SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
        if (renderer == nullptr)
        {
            logSDLError(std::cout, "Cannot create renderer.");
        }

        struct Graphic createbutton;
            createbutton.name = "Create Forest";
            createbutton.type = "longbutton";
            createbutton.x = 0;
            createbutton.y = 1 * (Screen_Width / 5) + 5;
            createbutton.h = Screen_Width / 10;
            createbutton.w = Screen_Width / 5;

        SDL_Event e;
            //For tracking if we want to quit
            bool quit = false;
            while (!quit) {
                while (SDL_PollEvent(&e))
                {
                    switch (e.type)
                    {
                    case SDL_MOUSEMOTION:
                        mouse_x = e.motion.x;
                        mouse_y = e.motion.y;
                        if (mouseOverRect(mouse_x, mouse_y, createbutton))
                            useCreate_Clip = CREATE_HOVER;
                        else
                            useCreate_Clip = CREATE_DEFAULT;
                        break;
                    }

        renderTexture(longbutton_image, renderer, createbutton, &create_clips[useCreate_Clip]);
    }

None 的结构成员已初始化。它们在初始化完成后分配给。

在声明结构时初始化结构,例如

Graphic createbutton {
    "Create Forest",
    "longbutton",
    0,
    1 * (Screen_Width / 5) + 5,
    Screen_Width / 10,
    Screen_Width / 5
};

请注意,此处不需要 struct 关键字。