簡體   English   中英

C++ 中的“類”類型重定義錯誤 C2011

[英]"Class" type redefinition error C2011 in C++

這是我的頭文件:

#ifndef SOMETHING_H
#define SOMETHING_H
#include <SDL.h>
#include <stdio.h>
#include <string>
#pragma once
class Something {
    
public:
    
    //Screen dimension constants
    int SCREEN_WIDTHS;
    int SCREEN_HEIGHTS;

    //Starts up SDL and creates window
    bool inits();

    //Loads media
    bool loadMediases();

    //Frees media and shuts down SDL
    void closes();

    Something(int height);

    int mains();

    //Loads individual image
    SDL_Surface* loadSurfaces(std::string path);

    //The window we'll be rendering to
    SDL_Window* gWindows;

    //The surface contained by the window
    SDL_Surface* gScreenSurfaces;

    //The images that correspond to a keypress
    SDL_Surface* gKeyPressSurfaceses[5];

    //Current displayed image
    SDL_Surface* gCurrentSurfaces;
};
#endif

這是我的 cpp 文件:

//Using SDL, standard IO, and strings

#include <SDL.h>
#include <stdio.h>
#include <string>
#include "Something.h"
class Something {
    
public:
    
    //Screen dimension constants
    int SCREEN_WIDTHS = 640;
    int SCREEN_HEIGHTS = 480;

    //Key press surfaces constants
    enum KeyPressSurfaceses
    {
        KEY_PRESS_SURFACE_DEFAULT,
        KEY_PRESS_SURFACE_UP,
        KEY_PRESS_SURFACE_DOWN,
        KEY_PRESS_SURFACE_LEFT,
        KEY_PRESS_SURFACE_RIGHT,
        KEY_PRESS_SURFACE_TOTAL
    };


    //The window we'll be rendering to
    SDL_Window* gWindows = NULL;

    //The surface contained by the window
    SDL_Surface* gScreenSurfaces = NULL;

    //The images that correspond to a keypress
    SDL_Surface* gKeyPressSurfaceses[KEY_PRESS_SURFACE_TOTAL];

    //Current displayed image
    SDL_Surface* gCurrentSurfaces = NULL;

    Something(int height) {
        SCREEN_HEIGHTS = height;
    }

    bool inits()
    {
        //Initialization flag
        bool success = true;

        //Initialize SDL
        if (SDL_Init(SDL_INIT_VIDEO) < 0)
        {
            printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
            success = false;
        }
        else
        {
            //Create window
            gWindows = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTHS, SCREEN_HEIGHTS, SDL_WINDOW_SHOWN);
            if (gWindows == NULL)
            {
                printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
                success = false;
            }
            else
            {
                //Get window surface
                gScreenSurfaces = SDL_GetWindowSurface(gWindows);
            }
        }

        return success;
    }

    bool loadMedias()
    {
        //Loading success flag
        bool success = true;

        //Load default surface
        gKeyPressSurfaceses[KEY_PRESS_SURFACE_DEFAULT] = loadSurfaces("resources/images/press.bmp");
        if (gKeyPressSurfaceses[KEY_PRESS_SURFACE_DEFAULT] == NULL)
        {
            printf("Failed to load default image!\n");
            success = false;
        }

        //Load up surface
        gKeyPressSurfaceses[KEY_PRESS_SURFACE_UP] = loadSurfaces("resources/images/up.bmp");
        if (gKeyPressSurfaceses[KEY_PRESS_SURFACE_UP] == NULL)
        {
            printf("Failed to load up image!\n");
            success = false;
        }

        //Load down surface
        gKeyPressSurfaceses[KEY_PRESS_SURFACE_DOWN] = loadSurfaces("resources/images/down.bmp");
        if (gKeyPressSurfaceses[KEY_PRESS_SURFACE_DOWN] == NULL)
        {
            printf("Failed to load down image!\n");
            success = false;
        }

        //Load left surface
        gKeyPressSurfaceses[KEY_PRESS_SURFACE_LEFT] = loadSurfaces("resources/images/left.bmp");
        if (gKeyPressSurfaceses[KEY_PRESS_SURFACE_LEFT] == NULL)
        {
            printf("Failed to load left image!\n");
            success = false;
        }

        //Load right surface
        gKeyPressSurfaceses[KEY_PRESS_SURFACE_RIGHT] = loadSurfaces("resources/images/right.bmp");
        if (gKeyPressSurfaceses[KEY_PRESS_SURFACE_RIGHT] == NULL)
        {
            printf("Failed to load right image!\n");
            success = false;
        }

        return success;
    }

    void closes()
    {
        //Deallocate surfaces
        for (int i = 0; i < KEY_PRESS_SURFACE_TOTAL; ++i)
        {
            SDL_FreeSurface(gKeyPressSurfaceses[i]);
            gKeyPressSurfaceses[i] = NULL;
        }

        //Destroy window
        SDL_DestroyWindow(gWindows);
        gWindows = NULL;

        //Quit SDL subsystems
        SDL_Quit();
    }

    SDL_Surface* loadSurfaces(std::string path)
    {
        //Load image at specified path
        SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
        if (loadedSurface == NULL)
        {
            printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
        }

        return loadedSurface;
    }


    int mains(int argc, char* args[])
    {
        //Start up SDL and create window
        if (!inits())
        {
            printf("Failed to initialize!\n");
        }
        else
        {
            //Load media
            if (!loadMedias())
            {
                printf("Failed to load media!\n");
            }
            else
            {
                //Main loop flag
                bool quit = false;

                //Event handler
                SDL_Event e;

                //Set default current surface
                gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_DEFAULT];

                //While application is running
                while (!quit)
                {
                    //Handle events on queue
                    while (SDL_PollEvent(&e) != 0)
                    {
                        //User requests quit
                        if (e.type == SDL_QUIT)
                        {
                            quit = true;
                        }
                        //User presses a key
                        else if (e.type == SDL_KEYDOWN)
                        {
                            //Select surfaces based on key press
                            switch (e.key.keysym.sym)
                            {
                            case SDLK_UP:
                                gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_UP];
                                break;

                            case SDLK_DOWN:
                                gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_DOWN];
                                break;

                            case SDLK_LEFT:
                                gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_LEFT];
                                break;

                            case SDLK_RIGHT:
                                gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_RIGHT];
                                break;

                            default:
                                gCurrentSurfaces = gKeyPressSurfaceses[KEY_PRESS_SURFACE_DEFAULT];
                                break;
                            }
                        }
                    }

                    //Apply the current image
                    SDL_BlitSurface(gCurrentSurfaces, NULL, gScreenSurfaces, NULL);

                    //Update the surface
                    SDL_UpdateWindowSurface(gWindows);
                }
            }
        }

        //Free resources and close SDL
        closes();

        return 0;
    }
};

為什么它不斷拋出“類”類型重定義錯誤? 我怎樣才能解決這個問題? 我已經嘗試了一切,但我遇到了更多問題。 我看到一些帖子將這個問題與定義類兩次有關,但是在 cpp 中去掉類定義並使用 classname::functionname 只會導致更多錯誤。

我知道這是一個愚蠢的問題,我是使用教程和 Visual Studio 的 C++ 初學者。

在您的 .cpp 文件中,您已經完全重新聲明了class Something 如果您只想將函數的實現放在 .cpp 文件中,那不是您這樣做的方式。

那里的語法應該是這樣的:

bool Something::inits()
{
    // implementation ...
}

bool Something::loadMedias()
{
    // implementation ...
}

等等

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM