C++ 错误,从函数调用类成员

标签 c++ class sdl

我正在构建游戏,但遇到编译错误 apply_surface was not declared in this scope. error is in line 95, in the function User::show().

编辑:现在它已确定,图像“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() 函数未在范围内声明

所以移动你的代码:

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 );
}

到顶部(在你使用它之前)

否则

像这样在顶部添加一个函数原型(prototype):

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

关于C++ 错误,从函数调用类成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19750496/

相关文章:

c++ - 有没有办法找到任意类类型的直接基类(如果有的话)?

c++ - SDL_ConvertSurface() 导致中断

除非使用 cout,否则 C++ 拒绝正常运行

c++ - C++ 中的右值引用和一个简单的例子

c++ - 如何为一般情况编写流插入运算符? (也就是说,对于 `char` 和 `wchar_t` 流?)

c++ - 如何将多个源文件转换为一个 .a 文件

java - 我应该给一个只有 getter 的 Java 类起什么名字?

scala - 创建类型敏感函数而不更改父特征或案例类

python 从字符串创建对象

c++ - 为什么我的程序使用的内存在运行时不断增长?