C ++错误,从函数调用类成员 [英] C++ error, Calling a class member from a function

查看:114
本文介绍了C ++错误,从函数调用类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助...拼命地。我正在制作一个游戏,我得到一个编译错误 apply_surface没有在这个范围内声明。 错误在第95行,在函数 User :: show()。我不知道我做错了什么。

I Need help... desperately. I am working on building a game, and I get a compiling error apply_surface was not declared in this scope. the error is in line 95, in the function User::show(). I don't know what I am doing wrong.

编辑:现在它被微调,图像阴茎,不会出现在屏幕上。

Now that it is fized, the image "penis", is not showing up on the screen. sorry for the edit.

/*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;
}


推荐答案

您的 apply_surface()函数未在范围内声明

your apply_surface() function is not declared within the scope

因此请移动您的代码:

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

到顶部(在使用它之前)

to the top ( before you use it )

在顶部添加一个函数原型,如下所示:

Add a function prototype at the top like this :

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

这篇关于C ++错误,从函数调用类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆