5

我的类 GraphicsManager 出现错误。

图形管理器.cpp:

#include "C:\Users\Chris Uzzolina\Desktop\obj\include\GraphicsManager.h"
#include <SDL.h>
#include <string>
GraphicsManager::GraphicsManager(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP,  std::string caption, SDL_Surface *screen)
{

}

GraphicsManager::~GraphicsManager()
{
    //dtor
}
bool init(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, std::string caption, SDL_Surface *scr)
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    scr = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( scr == NULL )
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Event test", NULL );

    //If everything initialized fine
    return true;
}

SDL_Surface *load_image( std::string filename )
{
    //Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;
    //Load the image
    loadedImage = SDL_LoadBMP( filename.c_str() );
    //If nothing went wrong in loading the image
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }
    //Return the optimized image
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;

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

void clean_up(SDL_Surface *image) 
{
    //Free the image
    SDL_FreeSurface( image );
}

void quit_sdl()
{
    SDL_Quit();
}

图形管理器.h:

#ifndef GRAPHICSMANAGER_H
#define GRAPHICSMANAGER_H
#include <string>
#include<SDL.h>
class GraphicsManager
{
public:
    GraphicsManager();
    GraphicsManager(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, std::string caption, SDL_Surface *screen);
    virtual ~GraphicsManager();
    bool init(int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP, std::string caption, SDL_Surface *screen );
    SDL_Surface *load_image(std::string filename);
    void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination );
    void clean_up(SDL_Surface *image);
    void quit_sdl();
protected:
private:
};

#endif // GRAPHICSMANAGER_H

SDLLesson01.cpp:

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include "C:\Users\Chris Uzzolina\Desktop\obj\include\GraphicsManager.h"

int SCREEN_WIDTH = 640;
int SCREEN_HEIGHT = 480;
int SCREEN_BPP = 32;
SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
std::string caption = "THProject";
SDL_Event event;

int main( int argc, char* args[] )
{
    bool quit=false;
    GraphicsManager grmanager();
    grmanager.init(SCREEN_WIDTH , SCREEN_HEIGHT , SCREEN_BPP , caption , screen);
    message = grmanager.load_image( "riku.bmp" );
    background = grmanager.load_image( "abc.bmp" );
    grmanager.apply_surface( 0, 0, background, screen );
    grmanager.apply_surface( 320, 0, background, screen );
    grmanager.apply_surface( 0, 240, background, screen );
    grmanager.apply_surface( 320, 240, background, screen );
    grmanager.apply_surface( 180, 140, message, screen );
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }
    while(quit==false)
    {
        while(SDL_PollEvent(&event))
        {
            if (event.type== SDL_QUIT)
            {
                quit=true;
            }
        }
    }
    grmanager.clean_up(message);
    grmanager.clean_up(background);
    grmanager.quit_sdl();
    return 0;
}

到目前为止,我已经在这个网站上搜索了多个错误,并且被我最新的错误难住了。如果有人可以对这个问题提供一些见解,将不胜感激。我正在运行 Windows Vista 并Code::Blocksmingw编译器和SDL库一起使用。这是构建消息:

C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp||In function 'int SDL_main(int, char**)':|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|20|error: request for member 'init' in 'grmanager', which is of non-class type 'GraphicsManager()'|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|21|error: request for member 'load_image' in 'grmanager', which is of non-class type 'GraphicsManager()'|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|22|error: request for member 'load_image' in 'grmanager', which is of non-class type 'GraphicsManager()'|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|23|error: request for member 'apply_surface' in 'grmanager', which is of non-class type 'GraphicsManager()'|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|24|error: request for member 'apply_surface' in 'grmanager', which is of non-class type 'GraphicsManager()'|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|25|error: request for member 'apply_surface' in 'grmanager', which is of non-class type 'GraphicsManager()'|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|26|error: request for member 'apply_surface' in 'grmanager', which is of non-class type 'GraphicsManager()'|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|27|error: request for member 'apply_surface' in 'grmanager', which is of non-class type 'GraphicsManager()'|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|42|error: request for member 'clean_up' in 'grmanager', which is of non-class type 'GraphicsManager()'|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|43|error: request for member 'clean_up' in 'grmanager', which is of non-class type 'GraphicsManager()'|
C:\Users\Chris Uzzolina\Desktop\obj\SDLLesson01.cpp|44|error: request for member 'quit_sdl' in 'grmanager', which is of non-class type 'GraphicsManager()'|
||=== Build finished: 11 errors, 0 warnings ===|
4

1 回答 1

0

在您的代码中,您有:

GraphicsManager grmanager();

您认为这是一个名为 grmanager 的 GraphicsManager 类型的 var 声明,并强制调用默认构造函数。但它确实是一个名为 grmanager 的函数声明,它返回一个 GraphicsManager 并且不带任何参数。所以你的编译器不理解

grmanager.Init(...)

至于它,grmanager 是一个函数名。

尝试用简单的 GraphicsManager grmanager 替换您的声明;

应该没问题,如果你不将参数传递给构造函数,它是被调用的默认值(除非你有一个黑暗的灵魂并且只创建了一个只有带默认值的参数的构造函数)

于 2012-11-10T10:10:01.790 回答