2016-07-11 21 views
0

Ich gehe durch das Tutorial auf Lazyfoo: 'Beginning Game Programming'. Ich habe gerade Tutorial Nummer 4.C++ Spiel Programmierung. Fehler: erwartet ')' vor ':' token

Mein Problem abgeschlossen ist wie folgt:

Der Code fein abgesehen von der Linie funktioniert:

SDL_Surface* loadSurface(std::string path); 

Der Fehler lautet:

error: expected ')' before ':' token

Ich bin zu dem Schluss gekommen, dass der Fehler etwas mit den Headern zu tun haben könnte. Es ist möglich, dass ich etwas zum Header SDL.h hinzufügen sollte.

Ich habe auch die stdbool.h Header hinzugefügt, um ein separates Problem zu beheben. Ich frage mich, ob das Probleme verursacht hat.

Hier ist der vollständige Code, der nur das Tutorial-Code ist (EDIT: Ich habe die problematische Spiel setzen in fett) (oder zumindest die Sterne um sie gegangen sind Erscheint nicht bolding werden innerhalb. . der Code Es ist in der Nähe von Anfang an, Zeile 33):

//Using SDL and standard IO 
#include <SDL.h> 
#include <stdio.h> 
#include <stdbool.h> 
#include <string.h> 
#include <stdlib.h> 

//Screen dimension constants 
const int SCREEN_WIDTH = 640; 
const int SCREEN_HEIGHT = 480; 

//Key press surfaces constants 
enum KeyPressSurfaces 
{ 
    KEY_PRESS_SURFACE_DEFAULT, 
    KEY_PRESS_SURFACE_UP, 
    KEY_PRESS_SURFACE_DOWN, 
    KEY_PRESS_SURFACE_LEFT, 
    KEY_PRESS_SURFACE_RIGHT, 
    KEY_PRESS_SURFACE_TOTAL 
}; 

//Starts up SDL and creates window 
bool init(); 

//Loads media 
bool loadMedia(); 

//Frees media and shuts down SDL 
void close(); 

//Loads individual image 
**SDL_Surface* loadSurface(std::string path);** 

//The window we'll be rendering to 
SDL_Window* gWindow = NULL; 

//The surface contained by the window 
SDL_Surface* gScreenSurface = NULL; 

//The images that correspond to a keypress 
SDL_Surface* gKeyPressSurfaces[ KEY_PRESS_SURFACE_TOTAL ]; 

//Current displayed image 
SDL_Surface* gCurrentSurface = NULL; 

bool init() 
{ 
    //Initialization flag 
    bool success = true; 

    //Initialize SDL 
    if(SDL_Init(SDL_INIT_VIDEO) < 0) 
    { 
     printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); 
     success = false; 
    } 
    else 
    { 
     //Create window 
     gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); 
     if(gWindow == NULL) 
     { 
      printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); 
      success = false; 
     } 
     else 
     { 
      //Get window surface 
      gScreenSurface = SDL_GetWindowSurface(gWindow); 
      } 
     } 

     return success; 
} 

bool loadMedia() 
{ 
    //Loading success flag 
    bool success = true; 

    //Load default surface 
    gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ] = loadSurface("04_key_presses/press.bmp"); 
    if(gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ] == NULL) 
    { 
     printf("Failed to load default image!\n"); 
     success = false; 
    } 

    //Load up surface 
    gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ] = loadSurface("04_key_presses/up.bmp"); 
    if(gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ] == NULL) 
    { 
     printf("Failed to load up image!\n"); 
     success = false; 
    } 

    //Load down surface 
    gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ] = loadSurface("04_key_presses/down.bmp"); 
    if(gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ] == NULL) 
    { 
     printf("failed to load down image!\n"); 
     success = false; 
    } 

    //Load left surface 
    gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ] = loadSurface("04_key_presses/left.bmp"); 
    if(gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ] == NULL) 
    { 
     printf("failed to load left image!\n"); 
     success = false; 
    } 

    //Load right surface 
    gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ] = loadSurface("04_key_presses/right.bmp"); 
    if(gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ] == NULL) 
    { 
     printf("Failed to load left image!\n"); 
     success = false; 
    } 

    return success; 
} 

void close() 
{ 
    int i; 
    //Deallocate surface 
    for(i < KEY_PRESS_SURFACE_TOTAL; ++i;) 
    { 
    SDL_FreeSurface(gKeyPressSurfaces[ i ]); 
    gKeyPressSurfaces[ i ] = NULL; 
    } 

    //Destroy window 
    SDL_DestroyWindow(gWindow); 
    gWindow = NULL; 

    //Quit SDL subsystems 
    SDL_Quit(); 
} 

SDL_Surface* loadSurface(std::string path) 
{ 
    //Load image at specified path 
    SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str()); 
    if(loadedSurface == NULL) 
    { 
     printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError()); 
    } 

    return loadedSurface; 
} 

int main(int argc, char* args[]) 
{ 

    //Start up SDL and create window 
    if(!init()) 
    { 
     printf("Failed to initialize!\n"); 
    } 
    else 
    { 
     //Load media 
     if(!loadMedia()) 
     { 
      printf("Failed to load media!\n"); 
     } 
     else 
     { 
      //Main loop flag 
      bool quit = false; 

      //Event handler 
      SDL_Event e; 

      //Set default current surface 
      gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ]; 

      //While application is running 
      while(!quit) 
      { 
       //Handle events on queue 
       while(SDL_PollEvent(&e) != 0) 
       { 
        //User requests quit 
        if(e.type == SDL_QUIT) 
        { 
         quit = true; 
        } 
        //User presses a key 
        else if(e.type == SDL_KEYDOWN) 
        { 
         //Select surfaces based on key press 
         switch(e.key.keysym.sym) 
         { 
         case SDLK_UP: 
         gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ]; 
         break; 

         case SDLK_DOWN: 
         gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ]; 
         break; 

         case SDLK_LEFT: 
         gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ]; 
         break; 

         case SDLK_RIGHT: 
         gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ]; 
         break; 

         default: 
         gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ]; 
         break; 
         } 
        } 
        } 

       //Apply the current image 
       SDL_BlitSurface(gCurrentSurface, NULL, gScreenSurface, NULL); 

       //Update the surface 
       SDL_UpdateWindowSurface(gWindow); 
        } 

       } 
    } 

    //Free resources and close SDL 
    close(); 

    return 0; 

} 
+2

Der Compiler hat Ihnen mitgeteilt, in welcher Zeile der Fehler aufgetreten ist. Könnten Sie es für uns markieren. # – Sean

+4

Fügen Sie '#include ' zu Ihrer Liste von Includes hinzu –

+0

'' und '' sind nicht das Gleiche. 'std :: string' befindet sich in' '. – NathanOliver

Antwort

2

Wie schon erwähnt, sollten Sie versuchen, die C++ - Zeichenfolge einzufügen.

#include <string> 

jedoch, wenn das Ihr Problem ist, sollte die Compiler gesagt hat string ist nicht im std Namespace.


Für mich sieht es aus wie der Compiler nicht über den :: Namespace Betreiber nicht kennt.

Die mögliche Ursache ist, dass Sie einen C-Compiler anstelle eines C++ - Compilers verwenden.

C hat keine Vorstellung von Namespace, und es hat keine std::string.


Stellen Sie sicher, Ihre Erweiterung der Quelldatei ist eine C++ ein (wie .cpp) im Gegensatz zu C (.c).

Abhängig von Ihrem Compiler, könnte man es sagen müssen meinen Sie C++ und nicht C. Wenn Sie mit gcc, versuchen g ++ statt.

+1

Dies scheint es behoben zu haben, danke :) Ich benutzte den falschen Header. Ich sollte enthalten, und aus irgendeinem Grund war die gesamte Datei .c anstelle von .cpp. Vielen Dank für die Hilfe und andere Probleme hingewiesen :) – elsaess

+0

Was tat? Um 'std :: string' einzuschließen? –

+1

Std :: String ist in enthalten, aber das war Teil eines größeren Problems. Sie hatten recht, dass der Compiler den :: Namespace-Operator nicht kannte. Das Dateiformat wurde korrigiert und die Kopfzeile geändert. – elsaess

3

Versuchen:

#include <string> 

statt:

#include <string.h>