收到 "error C2011: 'TextureManager' : 'class' type redefinition" 错误。 #ifndef #define #endif 在文件中
Receiving "error C2011: 'TextureManager' : 'class' type redefinition" error. #ifndef #define #endif in files
我正在按照 "SDL Game Development By Shaun Mitchell" 这本书学习 SDL2,但遇到了一个我无法解决的编译器错误。我在尝试构建项目时收到 "error C2011: 'TextureManager' : 'class' type redefinition"。我使用的是 Visual Studio 2008。我添加了通常针对此错误建议的预编译命令。我已经清理了项目然后构建了它。每次我尝试构建项目时,错误都会继续。
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
#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
#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
#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
#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;
}
看起来 TextureManager
的包含保护不正确(#ifndef
检查中的拼写错误。
你有:
#ifndef __TextureManger__
#define __TextureManager__
.
.
.
#endif
应该是:
#ifndef __TextureManager__
#define __TextureManager__
.
.
.
#endif
我正在按照 "SDL Game Development By Shaun Mitchell" 这本书学习 SDL2,但遇到了一个我无法解决的编译器错误。我在尝试构建项目时收到 "error C2011: 'TextureManager' : 'class' type redefinition"。我使用的是 Visual Studio 2008。我添加了通常针对此错误建议的预编译命令。我已经清理了项目然后构建了它。每次我尝试构建项目时,错误都会继续。
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
#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
#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
#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
#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;
}
看起来 TextureManager
的包含保护不正确(#ifndef
检查中的拼写错误。
你有:
#ifndef __TextureManger__
#define __TextureManager__
.
.
.
#endif
应该是:
#ifndef __TextureManager__
#define __TextureManager__
.
.
.
#endif