簡體   English   中英

無法從'SDL_Surface'轉換為'SDL_Surface MainMenu :: *'

[英]cannot convert from 'SDL_Surface' to 'SDL_Surface MainMenu::*'

情況:我正在嘗試在MainMenu類中使用load_image方法,所以在我的主類中,我可以在主菜單中調用load_image方法。

問題:load_image方法中,我收到錯誤:

錯誤C2440:'return':無法從'SDL_Surface'轉換為'SDL_Surface MainMenu :: *'

這發生在第47行,用紅色標出“optimizeImage”。

這是頭文件:

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM