【问题标题】:C++ error, Calling a class member from a functionC++ 错误,从函数调用类成员
【发布时间】:2013-11-03 06:43:56
【问题描述】:

我正在开发一款游戏,但在函数User::show() 中出现编译错误apply_surface was not declared in this scope. 错误在第95 行

编辑:现在它已经完成,图像“baz”没有出现在屏幕上。抱歉编辑。

/*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 *baz = NULL;
SDL_Event event;

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

bool quit = false;

class User
{
private:
    SDL_Surface *baz = 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,baz,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");
    baz = IMG_Load("baz.png");
    apply_surface(0,0,background,screen);
    
    SDL_Flip(screen);
    
    while( quit == false )
    {
        User Baz;
        Baz.user();
        Baz.keys();
        Baz.move();
        Baz.show();
        if(quit == true)
        {
            cleanup();
        }        
    }
    return 0;
}

【问题讨论】:

  • apply_surface()的定义移到使用它的函数上方。

标签: c++ class sdl


【解决方案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*);

【讨论】:

  • 如果您将参数包含在函数中,这个答案会更好。
  • 照你说的做了@kfsone
  • 另一种选择是在他第一次使用它之前添加一个原型:apply_surface(x,y,baz,screen);
  • 对不起,笑得太厉害了,他照原样发布了……void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination );
  • 是的当然@kfsone,但我告诉的方式甚至不需要那段额外的代码......无论如何我也会包括你所说的方式......谢谢你的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多