Question

i am following SDL game development book by shaun mitcchel and was trying to compile the code and was getting error of constructor is private. i am using mingw for compiling.

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

errors C:\Users\lenovo\Desktop\node project\sdl\TextureManager.h||In constructor 'game::game()':|

 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*&)'|
Was it helpful?

Solution

You have textureManager m_textureManager member in class game (file game.h) But in game.cpp you always use ThetextureManager::Instance()

You just need to remove m_textureManager member.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top