title: C++ error expected class-name before '{' token
body: <p>I know many, many others have posted about this but I am so lost, due to so many people saying forward declaration and other massively confusing things that I can't seem to find a way to use without bringing up new errors in my compiler so if anyone could help me that would be much appreciated.</p>
  
  <p>I am using Code::Blocks on Ubuntu 15.04</p>
  
  <p>The Error is <code>OptimizedSurface.h|11|error: expected class-name before ‘{’ token</code></p>
  
  <p>In the OptimizedSurface Header file I decided to do <code>class OptimizedSurface : Game{</code> because before this error I was getting </p>
  
  <p><code>include/Game.h|18|error: invalid use of member ‘Game::windowSurface’ in static member function</code> the line was optimized_surface = SDL_ConvertSurface(surface, Game::windowSurface->format, 0);</p>
  
  <p>And now the the code is the below files.</p>
  
  <p>main.cpp - </p>
  
  <pre><code>#include "Game.h"
  #include &lt;stdio.h&gt;
  
  
  int main(int argc, char* args[]){
      printf("Initializing Game class\n");
      Game game = Game();
      return 0;
  }
  </code></pre>
  
  <p>Game.h - </p>
  
  <pre><code>#ifndef GAME_H
  #define GAME_H
  
  #include "SDL2/SDL.h"
  #include "SDL2/SDL_image.h"
  #include &lt;stdio.h&gt;
  #include &lt;chrono&gt;
  #include &lt;thread&gt;
  #include &lt;iostream&gt;
  #include "MenuState.h"
  
  class MenuState;
  
  class Game{
      public:
          Game();
          virtual ~Game();
          SDL_Surface *windowSurface = nullptr;
  
      protected:
  
      private:
          void tick();
          void update_time();
          std::chrono::system_clock::time_point now, last_frame;
          int delta = 0;
          void event_handler();
          void render();
  
          // Screen Dimensions
          const int SCREEN_WIDTH = 640;
          const int SCREEN_HEIGHT = 480;
  
          //Main Window
          SDL_Window *window = nullptr;
  
          // Surfaces
          SDL_Surface *currentImage = nullptr;
  
          // Main Menu Screen
          MenuState *menu;
  
          bool isRunning = true;
  
          // Main Menu = 1; Game = 2; Paused = 3;
          int currentState = 1;
  
          // Events
          SDL_Event ev;
  };
  
  #endif // GAME_H
  </code></pre>
  
  <p>Game.cpp - </p>
  
  <pre><code>#include "Game.h"
  
  
  Game::Game(){
      // Initialize SDL
      if( SDL_Init( SDL_INIT_VIDEO ) &lt; 0 )
          printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
  
      else{
          this-&gt;menu = new MenuState;
          // Create Window
          this-&gt;window = SDL_CreateWindow( "Tetris", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
  
          if( this-&gt;window == NULL ){
              printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
          }
  
          else{
              // Get window surface
              this-&gt;windowSurface = SDL_GetWindowSurface( this-&gt;window );
  
              // Fill the surface white
              SDL_FillRect( this-&gt;windowSurface, NULL, SDL_MapRGB( this-&gt;windowSurface-&gt;format, 0xFF, 0xFF, 0xFF ) );
  
              // Update the Surface
              SDL_UpdateWindowSurface( this-&gt;window );
  
              //Set current image to display as main menu
              this-&gt;currentImage = this-&gt;menu-&gt;MenuSurface[ 0 ];
              this-&gt;tick();
          }
      }
  }
  
  void Game::tick(){
      while( this-&gt;isRunning ){
          if( !( this-&gt;delta &gt;= 33 ) )
              this-&gt;update_time();
          else{
              this-&gt;last_frame = std::chrono::system_clock::now();
              this-&gt;event_handler();
              this-&gt;render();
          }
      }
  }
  
  void Game::update_time(){
      this-&gt;now = std::chrono::system_clock::now();
      this-&gt;delta = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(this-&gt;now - this-&gt;last_frame).count();
  }
  
  void Game::event_handler(){
      while( SDL_PollEvent( &amp;this-&gt;ev ) != 0 &amp;&amp; this-&gt;currentState == 1){
          switch( this-&gt;ev.type ){
              case SDL_MOUSEBUTTONDOWN:
                  if( this-&gt;ev.button.button  == SDL_BUTTON_LEFT )
                      if( this-&gt;menu-&gt;checkPos( this-&gt;ev.button.x, this-&gt;ev.button.y ) )
                          this-&gt;currentImage = this-&gt;menu-&gt;MenuSurface[ 2 ];
                      break;
  
              case SDL_MOUSEBUTTONUP:
                  if( this-&gt;ev.button.button == SDL_BUTTON_LEFT )
                      if( this-&gt;menu-&gt;checkPos( this-&gt;ev.button.x, this-&gt;ev.button.y ) )
                          currentState = 2;
                          // Start the Game
              default:
                  if( this-&gt;menu-&gt;checkPos( this-&gt;ev.button.x, this-&gt;ev.button.y ) )
                      this-&gt;currentImage = this-&gt;menu-&gt;MenuSurface[ 1 ];
                  else
                      this-&gt;currentImage = this-&gt;menu-&gt;MenuSurface[ 0 ];
                  break;
          }
      }
      if( this-&gt;ev.type == SDL_QUIT )
          this-&gt;isRunning = false;
  }
  
  void Game::render(){
      SDL_BlitSurface( this-&gt;currentImage, NULL, windowSurface, NULL );
      SDL_UpdateWindowSurface( window );
  }
  
  Game::~Game(){
      // Destroy Everything
      this-&gt;currentImage = nullptr;
  
      SDL_DestroyWindow( this-&gt;window );
      this-&gt;window = nullptr;
  
      delete menu;
  
      // Quit SDL subsystems
      SDL_Quit();
  }
  </code></pre>
  
  <p>MenuState.h - </p>
  
  <pre><code>#ifndef MENUSTATE_H
  #define MENUSTATE_H
  
  #include &lt;SDL2/SDL.h&gt;
  #include &lt;SDL2/SDL_image.h&gt;
  #include "OptimizedSurface.h"
  #include &lt;string&gt;
  
  
  class MenuState{
      public:
          MenuState();
          virtual ~MenuState();
  
          bool checkPos( int x, int y );
  
          SDL_Surface *MenuSurface[ 3 ];
  
          int buttonX [ 2 ];
          int buttonY [ 2 ];
  
      protected:
  
      private:
  };
  
  #endif // MENUSTATE_H
  </code></pre>
  
  <p>MenuState.cpp - </p>
  
  <pre><code>#include "MenuState.h"
  
  
  MenuState::MenuState(){
      for( int i = 1; i &lt;= 3; ++i){
          this-&gt;MenuSurface[ i-1 ] = OptimizedSurface::convert( "assets/menu/" + std::to_string(i) + ".jpg" );
      }
      this-&gt;buttonX[ 0 ] = 250;
      this-&gt;buttonX[ 1 ] = buttonX[ 0 ] + 140;
  
      this-&gt;buttonY[ 0 ] = 36;
      this-&gt;buttonY[ 1 ] = buttonY[ 0 ] + 100;
  }
  
  bool MenuState::checkPos( int x, int y ){
      if( this-&gt;buttonX[ 0 ] &lt; x &amp;&amp; x &lt; this-&gt;buttonX[ 1 ] )
          if( this-&gt;buttonY[ 0 ] &lt; y &amp;&amp; y &lt; this-&gt;buttonY[ 1 ] )
              return true;
      return false;
  }
  
  MenuState::~MenuState(){
      for( int i = 0; i &lt; 3; ++i ){
          SDL_FreeSurface( this-&gt;MenuSurface[ i ] );
          this-&gt;MenuSurface[ i ] = nullptr;
      }
  }
  </code></pre>
  
  <p>OptimizedSurface.h - </p>
  
  <pre><code>#ifndef OPTIMIZEDSURFACE_H
  #define OPTIMIZEDSURFACE_H
  
  #include "SDL2/SDL.h"
  #include "SDL2/SDL_image.h"
  #include "Game.h"
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  
  
  class OptimizedSurface : public Game{
      public:
          OptimizedSurface();
          static SDL_Surface *convert( std::string filepath );
          virtual ~OptimizedSurface();
      protected:
      private:
  };
  
  #endif // OPTIMIZEDSURFACE_H
  </code></pre>
  
  <p>OptimizedSurface.cpp - </p>
  
  <pre><code>#include "OptimizedSurface.h"
  
  
  OptimizedSurface::OptimizedSurface(){
  }
  
  SDL_Surface *OptimizedSurface::convert( std::string filepath ){
      SDL_Surface *optimized_surface = nullptr;
      SDL_Surface *surface = IMG_Load(filepath.c_str());
      if( surface == NULL ){
          printf( "Error Optimizing Surface: %s\n", SDL_GetError() );
      }
      else{
          optimized_surface = SDL_ConvertSurface(surface, windowSurface-&gt;format, 0);
          if( optimized_surface == NULL )
              printf( "Error Optimizing Surface: %s\n", SDL_GetError() );
      }
      SDL_FreeSurface(surface);
  
      return optimized_surface;
  }
  
  OptimizedSurface::~OptimizedSurface(){
  }
  </code></pre>