2013-11-03 22 views
0

我需要幫助...絕望。我正在製作遊戲,並且我收到編譯錯誤apply_surface was not declared in this scope.錯誤是在行95,在功能User::show()。我不知道我做錯了什麼。C++錯誤,從函數調用類成員

編輯:現在它是fized,圖像「陰莖」,沒有顯示在屏幕上。抱歉編輯。

/*This source code copyrighted by Lazy Foo' Productions (2004-2013) 
and may not be redistributed without written permission.*/ 

//The headers 
#include "SDL/SDL.h" 
#include "SDL/SDL_image.h" 
#include <string> 

SDL_Surface *background = NULL; 
SDL_Surface *screen = NULL; 
SDL_Surface *penis = NULL; 
SDL_Event event; 

void cleanup() 
{ 
    SDL_FreeSurface(background); 
    SDL_FreeSurface(screen); 
    SDL_FreeSurface(penis); 
    SDL_Quit; 
} 

bool quit = false; 

class User 
{ 
private: 
    SDL_Surface *penis = NULL; 
    SDL_Surface *screen = NULL; 
    int x; 
    int y; 
    int xVel; 
    int yVel; 
public: 
    void keys(); 
    void move(); 
    void show(); 
    void user(); 
}; 
void User::keys() 
{ 
    while(quit == false) 
     if(SDL_PollEvent(&event)) 
     { 
      if(event.type == SDL_QUIT) 
       quit = true; 
     } 

    Uint8 *keystates = SDL_GetKeyState(NULL); 


    if(keystates[SDLK_d]) 
    { 
     User::xVel+=50; 
    } 
    if(keystates[SDLK_a]) 
    { 
     User::xVel-=50; 
    } 
    if(keystates[SDLK_s]) 
    { 
     User::yVel+=50; 
    } 
    if(keystates[SDLK_w]) 
    { 
     User::yVel-=50; 
    } 
    if(keystates[SDLK_ESCAPE]) 
    { 
     cleanup(); 
    } 
} 

void User::user() 
{ 
    x = 0; 
    y = 0; 
    xVel = 0; 
    yVel = 0; 
} 

void User::move() 
{ 
    x+=xVel; 
    y+=yVel; 
    x-=xVel; 
} 


void User::show() 
{ 
    apply_surface(x,y,penis,screen); 
    SDL_Flip(screen); 
} 

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) 
{ //Temporary rectangle to hold the offsets 

    SDL_Rect offset; 
    //Get the offsets 
    offset.x = x; 
    offset.y = y; //Blit the surface 
    SDL_BlitSurface(source, NULL, destination, &offset); 
} 



int main(int argc, char* argv[]) 
{ 
    SDL_Init(SDL_INIT_EVERYTHING); 
    screen = SDL_SetVideoMode(640,400,32,SDL_SWSURFACE); 
    background = IMG_Load("background.png"); 
    penis = IMG_Load("penis.png"); 
    apply_surface(0,0,background,screen); 

    SDL_Flip(screen); 

    while(quit == false) 
    { 
     User Penis; 
     Penis.user(); 
     Penis.keys(); 
     Penis.move(); 
     Penis.show(); 
     if(quit == true) 
     { 
      cleanup(); 
     }   
    } 
    return 0; 
} 
+3

將'apply_surface()'的定義移到使用它的函數上面。 –

+0

您贏得了StackOverflow:'SDL_Surface * penis = NULL;'。辣椒醫生到處去了。 – kfsone

+2

'用戶陰莖;''Penis.move();''Penis.show();'看起來像有趣的遊戲 –

回答

1

apply_surface()功能未範圍內聲明

所以,你的代碼移到:

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) 
{ //Temporary rectangle to hold the offsets 

    SDL_Rect offset; 
    //Get the offsets 
    offset.x = x; 
    offset.y = y; //Blit the surface 
    SDL_BlitSurface(source, NULL, destination, &offset); 
} 

頂端(使用前)

否則

在頂部添加一個函數原型,如下所示:

void apply_surface(int, int, SDL_Surface*, SDL_Surface*); 
+0

如果將參數包含在函數中,這個答案會更好。 – kfsone

+0

這樣做是因爲你說@kfsone –

+0

另一種選擇是,他只是在第一次使用它時添加了一個原型:'apply_surface(x,y,陰莖,屏幕);' – kfsone