c++ - 无法从 'SDL_Surface' 转换为 'SDL_Surface MainMenu::*'

标签 c++ class pointers sdl

情况:我正在尝试在 MainMenu 类中使用 load_image 方法,因此在我的主类中我可以调用 load_image 方法,同时在主菜单中。

问题:在 load_image 方法中,我收到错误:

error C2440: 'return' : cannot convert from 'SDL_Surface' to 'SDL_Surface MainMenu::*'

这发生在第 47 行,用红色强调“optimizedImage”。

这是头文件:

#ifndef MAINMENU_H
#define MAINMENU_H
#include <iostream>
#include <string>
#include "SDL.h"
#include "SDL_image.h"

using namespace std;

class MainMenu
{
    private:
        string menuOption[3];
        string background;
        string img1;
        string img2;
        string img3;
        SDL_Surface *screen;
        SDL_Surface *bkgrnd;
        SDL_Surface *item;

public:

    MainMenu(SDL_Surface *screen, string bgImg, string optionImg1, string optionImg2, string optionImg3) 
        : background(bgImg), img1(optionImg1), img2(optionImg2), img3(optionImg3)
    {

    }

    ~MainMenu();
    SDL_Surface  *load_image(string filename);
    void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination);
    bool load_files();
    void Draw();
};
#endif

这是 .cpp 文件:

#include <iostream>
#include <string>
#include "MainMenu.h"
#include "glut.h"
#include "SDL.h"
#include "SDL_image.h"

MainMenu::~MainMenu()
{
}


SDL_Surface  MainMenu::*load_image(string filename)
{
//Temporary storage for the image tgat loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load(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);

        //if the image was optimized just fine
        if (optimizedImage != NULL)
        {
            //Map the color key
            Uint32 colorkey = SDL_MapRGB( optimizedImage->format,0,0xFF,0xFF);
            //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
            SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
        }
        return optimizedImage;
    }
    return optimizedImage;
}

void MainMenu::apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
    SDL_Rect offset;
    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;

    SDL_BlitSurface(source, NULL, destination, &offset);
}

bool MainMenu::load_files()
{
    //Load images
    item = load_image(img1);
    bkgrnd = load_image(background);
    if (item == NULL || bkgrnd == NULL)
    {
        return false;
    }
    return true;
}

void MainMenu::Draw()
{
    apply_surface(0, 0, bkgrnd, screen);
    apply_surface(320, 0, bkgrnd, screen ); 
    apply_surface(0, 240, bkgrnd, screen ); 
    apply_surface(320, 240, bkgrnd, screen );

    apply_surface(180, 140, item, screen ); 
}

最佳答案

在您的 cpp 文件中,更改此内容:

SDL_Surface  MainMenu::*load_image(string filename)

对此:

SDL_Surface* MainMenu::load_image(string filename)

函数的返回类型是SDL_Surface*,不是SDL_Surface

关于c++ - 无法从 'SDL_Surface' 转换为 'SDL_Surface MainMenu::*',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18176520/

相关文章:

c++ - 如何在同一可变参数模板的不同实例之间进行转换?

c++ - 如何改变 boost::variant operator < 的行为

c++ - FireFox 通讯功能

c - 操作系统的低级指针处理

c - 声明的 int 上的指针操作数与声明的指针

c - C中通过函数跟踪指针对象的方法

C++ 示例项目 - 需要算法帮助

c++ - 不允许继承成员,为什么?

javascript - 具有默认参数值的 es6 类构造函数上的 NodeJS 错误

java - 从内联覆盖方法访问实例