构造函数是使用singleton c ++并在此上下文中出错的私有错误

时间:2014-03-29 21:00:10

标签: c++ mingw sdl singleton-methods

我正在关注shaun mitcchel的SDL游戏开发书,并且正在尝试编译代码,并且构造函数的错误是私有的。我正在使用mingw进行编译。

的main.cpp

#include "game.h"

game* g_game = 0;
int main(int argc, char* args[])
{
  g_game = new game;
  g_game->init("SDL",100,100,640,480,SDL_WINDOW_SHOWN);
  while(g_game->running())
  {
     g_game->handleEvents();
     g_game->update();
     g_game->render();
   }
  g_game->clean();
  return 0;
 }

game.h

#ifndef GAME_H
#define GAME_H

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

class game
{
 public:
    game(){}
    virtual ~game();
    bool init(char* title, int xpos, int ypos, int width, int height, int flags);
    void render();
    void update();
    void handleEvents();
    void clean();
    bool running()
    {
        return m_bRunning;
    }
 protected:
 private:
    SDL_Window* m_pWindow;
    SDL_Renderer* m_pRenderer;
    int m_currentFrame;
    textureManager m_textureManager;
    bool m_bRunning;
};

#endif // GAME_H

game.cpp

#include "game.h"


bool game::init(char* title,int xpos, int ypos, int width, int height,int flags)
{
 //initialize SDL
 if(SDL_Init(SDL_INIT_EVERYTHING)== 0)
 {
    std::cout << "SDL init success\n";
    //init window
    m_pWindow = SDL_CreateWindow(title,xpos,ypos,width,height,flags);
    if(m_pWindow != 0)
    {
        std::cout << "window creation successful\n";
        m_pRenderer = SDL_CreateRenderer(m_pWindow,-1,0);
        if(m_pRenderer != 0) {
            std::cout << "render init success\n";
            SDL_SetRenderDrawColor(m_pRenderer,255,0,0,255);
        }
        else {
            std::cout << "render init fail\n";
            return false;
        }
    }
    else {
        std::cout << "Window init fail\n";
        return false;
    }
   }
   else {
    std::cout << "Sdl init failed\n";
    return false;
  }
  std::cout << "init success\n";
  m_bRunning = true;
  //to load
  if(!ThetextureManager::Instance()->load("assets/animate-      alpha.png","animate",m_pRenderer))
  {
    return false;
  }
  }

void game::render()
{
SDL_RenderClear(m_pRenderer);
ThetextureManager::Instance()->draw("animate", 0, 0,128,82,m_pRenderer);
ThetextureManager::Instance()->drawFrame("animate", 100,100,128,82,1,m_currentFrame,m_pRenderer);
SDL_RenderPresent(m_pRenderer);
}
void game::handleEvents()
{
 SDL_Event event;
 if(SDL_PollEvent(&event)) {
    switch(event.type)
    {
    case SDL_QUIT:
        m_bRunning = false;
        break;
    default:
        break;
     }
   }
  }

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

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

TextureManager.h

#ifndef TEXTUREMANAGER_H_INCLUDED
#define TEXTUREMANAGER_H_INCLUDED

#include <iostream>
#include<map>
#include<SDL.h>
#include<SDL_image.h>

class textureManager {
public:
 static textureManager* Instance()
 {
    if(s_pInstance == 0)
    {
        s_pInstance = new textureManager();
        return s_pInstance;
    }
    return s_pInstance;
}
  bool load(std::string fileName, std::string id,SDL_Renderer* pRenderer);
  void draw(std::string id,int x,int y,int width,int height,SDL_Renderer* pRenderer,SDL_RendererFlip flip);
  void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer,SDL_RendererFlip flip);
  std::map<std::string, SDL_Texture*> m_textureMap;



 private:
  textureManager() {}
  textureManager(const textureManager&);
  textureManager& operator=(const textureManager&);

  static textureManager* s_pInstance;
};
typedef textureManager ThetextureManager;
#endif // TEXTUREMANAGER_H_INCLUDED

texturemanagement.cpp

#include "TextureManager.h"

textureManager* textureManager::s_pInstance = 0;

 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);

 if(pTexture != 0)
 {
    m_textureMap[id] = pTexture;
    return true;
 }
  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);
 }

错误     C:\ Users \ lenovo \ Desktop \ node project \ sdl \ TextureManager.h ||在构造函数&#39; game :: game()&#39;:|

 C:\Users\lenovo\Desktop\node project\sdl\TextureManager.h|28|error: 'textureManager::textureManager()' is private|
 C:\Users\lenovo\Desktop\node project\sdl\game.h|12|error: within this context|
 C:\Users\lenovo\Desktop\node project\sdl\game.cpp||In member function 'void game::render()':|
 C:\Users\lenovo\Desktop\node project\sdl\game.cpp|46|error: no matching function for call to 'textureManager::draw(const char [8], int, int, int, int, SDL_Renderer*&)'|
 C:\Users\lenovo\Desktop\node project\sdl\game.cpp|47|error: no matching function for call to 'textureManager::drawFrame(const char [8], int, int, int, int, int, int&, SDL_Renderer*&)'|

1 个答案:

答案 0 :(得分:1)

你在类游戏(文件game.h)中有textureManager m_textureManager成员 但是在game.cpp中你总是使用ThetextureManager :: Instance()

您只需要删除m_textureManager成员。