2017-06-15 29 views
0

當我編譯這個SDL代碼,我得到這個錯誤:SDL應用程序錯誤:預期不合格-ID之前 '如果'

SDL_DEV.cpp:60:2: error: expected unqualified-id before ‘if’ 


if(SDL_Init(SDL_INIT_VIDEO) < 0) 
^
SDL_DEV.cpp:64:2: error: expected unqualified-id before ‘else’ 
    else 
^
Makefile:21: recipe for target 'all' failed 
make: *** [all] Error 1 

(我使用g ++在Linux Mint的與SDL 2.0.5編譯安裝這個)

以下是完整的源代碼

#include <SDL2/SDL.h> 
#include <stdio.h> 


const int SCREEN_WIDTH = 640; 
const int SCREEN_HEIGHT = 480; 

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

    SDL_Window* window = NULL; 


    SDL_Surface* screenSurface = NULL; 


    if(SDL_Init(SDL_INIT_VIDEO) < 0) 
    { 
     printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); 
    } 
    else 
    { 

     window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); 
     if(window == NULL) 
     { 
      printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); 
     } 
     else 
     { 

      screenSurface = SDL_GetWindowSurface(window); 


      SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); 


      SDL_UpdateWindowSurface(window); 


      SDL_Delay(2000); 
     } 
    } 


    SDL_DestroyWindow(window); 


    SDL_Quit(); 

    return 0; 
} 
    SDL_Window* window = NULL; 


    SDL_Surface* screenSurface = NULL; 


    if(SDL_Init(SDL_INIT_VIDEO) < 0) 
    { 
     printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); 
    } 
    else 
    { 

     window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); 
     if(window == NULL) 
     { 
      printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); 
     } 
     else 
     { 

      screenSurface = SDL_GetWindowSurface(window); 


      SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF)); 


      SDL_UpdateWindowSurface(window); 


      SDL_Delay(2000); 
     } 
    } 


    SDL_DestroyWindow(window); 


    SDL_Quit(); 

    return 0; 
} 

我不明白爲什麼我收到此錯誤,因爲我以前編譯它在不同的機器上。如果有人能幫我解決這個問題,我會非常高興。謝謝。

+3

嗯,在'main'結束後你的代碼完全一樣......你是否意外地複製粘貼了'main'內容? – VTT

回答

0

問題解決了,我想我只是沒有看到額外的代碼,當我從我的Windows平臺複製它。

相關問題