简体   繁体   English

收到“错误C2011:'TextureManager':'类'类型重新定义”错误。 #ifndef #define #endif在文件中

[英]Receiving “error C2011: 'TextureManager' : 'class' type redefinition” error. #ifndef #define #endif in files

I am following the book "SDL Game Development By Shaun Mitchell" to learn SDL2 and have run into a compiler error I cannot solve. 我正在阅读《 Shaun Mitchell的SDL游戏开发》这本书,以学习SDL2,并遇到了我无法解决的编译器错误。 I am receiving a "error C2011: 'TextureManager' : 'class' type redefinition" when attempting to build the project. 尝试生成项目时,我收到“错误C2011:'TextureManager':'class'类型重新定义”。 I am using Visual Studio 2008. I have added the precompile commands as typically suggested for this error. 我使用的是Visual Studio2008。我已针对该错误添加了通常建议的预编译命令。 I have cleaned the project then built it. 我已经清理了项目,然后构建了它。 The error continues every time I attempt to build the project. 每当我尝试构建项目时,错误都会继续。

TextureManager.h TextureManager.h

#include "SDL.h"
#include <iostream>
#include <map>
#include <string>

#ifndef __TextureManger__
#define __TextureManager__


class TextureManager
{
    public:
        bool load(std::string fileName,std::string id,SDL_Renderer* pRenderer);
    // draw
    void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);

    // drawframe
    void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);

    std::map<std::string, SDL_Texture*> m_textureMap;
};


#endif /* defined(__TextureManager__) */

TextureManager.cpp TextureManager.cpp

#include "TextureManager.h"
#include "SDL_image.h"
#include "SDL.H"

bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer)
{
SDL_Surface* pTempSurface = IMG_Load(fileName.c_str());
if(pTempSurface == 0)
{
    return false;
}
SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface);
SDL_FreeSurface(pTempSurface);
// everything went ok, add the texture to our list
if(pTexture != 0)
{
    m_textureMap[id] = pTexture;
    return true;
}
// reaching here means something went wrong
return false;
}

void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x = 0;
srcRect.y = 0;
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
destRect.x = x;
destRect.y = y;
SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip);
}

void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip)
{
SDL_Rect srcRect;
SDL_Rect destRect;
srcRect.x = width * currentFrame;
srcRect.y = height * (currentRow - 1);
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
destRect.x = x;
destRect.y = y;
SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip);
} 

Game.h Game.h

#include "SDL.h"
#include "TextureManager.h"
#include <iostream>

#ifndef __Game__
#define __Game__

class Game
{
public:
    Game() {}
    ~Game() {}
    // simply set the running variable to true
    bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
    void render();
    void update();
    void handleEvents();
    void clean();

    // a function to access the private running variable
    bool running()
    { 
            return m_bRunning; 
    }
private:
    SDL_Window* m_pWindow;
    SDL_Renderer* m_pRenderer;
    int m_currentFrame;
    TextureManager m_textureManager;
    bool m_bRunning;
};
#endif /* defined(__Game__) */ 

Game.cpp Game.cpp

#include "Game.h"
#include "SDL_image.h"
#include "TextureManager.h"

//defined init function
bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
int flags = 0;
if(fullscreen)
{
    flags = SDL_WINDOW_FULLSCREEN;
}
//attempt to initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
    std::cout << "SDL init success\n";
    // init the window
    m_pWindow = SDL_CreateWindow(title, xpos, ypos,
    width, height, flags);
    if(m_pWindow != 0) // window init success
    {
        std::cout << "window creation success\n";
        m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
        if(m_pRenderer != 0) // renderer init success
        {
            std::cout << "renderer creation success\n";
            SDL_SetRenderDrawColor(m_pRenderer,255,0,0,255);
        }
        else
        {
            std::cout << "renderer init fail\n";
            return false; // renderer init fail
        }
    }
    else
    {
        std::cout << "window init fail\n";
        return false; // window init fail
    }
}
else
{
    std::cout << "SDL init fail\n";
    return false; // SDL init fail
}
std::cout << "init success\n";
//Load the image
m_textureManager.load("assets/animate-alpha.png", "animate", m_pRenderer);
m_bRunning = true; // everything inited successfully, start the main loop
return true;
}

//define render function
void Game::render()
{
SDL_RenderClear(m_pRenderer); //clear
m_textureManager.draw("animate", 0,0, 128, 82, m_pRenderer);
m_textureManager.drawFrame("animate", 100,100, 128, 82, 1, m_currentFrame, m_pRenderer);
SDL_RenderPresent(m_pRenderer); //draw
}


//defining clean function
void Game::clean()
{
std::cout << "cleaning game\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}

//handle event function
void Game::handleEvents()
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
    switch (event.type)
    {
        case SDL_QUIT:
            m_bRunning = false;
            break;
        default:
            break;
    }
}
}

void Game::update()
{
m_currentFrame = 128 * int(((SDL_GetTicks() / 100) % 6));
}

Main.cpp Main.cpp

#include<SDL.h>
#include <iostream>
#include "Game.h"
// our Game object

Game* g_game = 0;
int main(int argc, char* argv[])
{
    g_game = new Game();
    g_game->init("Chapter 1", 100, 100, 640, 480, 0);
    while(g_game->running())
{
    g_game->handleEvents();
    g_game->update();
    g_game->render();
}

g_game->clean();
return 0;
} 

It looks like the include guards for TextureManager are incorrect (incorrect spelling in the #ifndef check. 看来TextureManager的包含保护TextureManager不正确( #ifndef检查中的拼写不正确。

You have: 你有:

#ifndef __TextureManger__
#define __TextureManager__
.
.
.
#endif

It should be: 它应该是:

#ifndef __TextureManager__
#define __TextureManager__
.
.
.
#endif

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

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