无法从'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主菜单::*'
这发生在第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
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- SDL 窗口不会弹出
- SDL 操纵杆按钮按下/C++
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- SDL 映像:无法打开映像,仅显示错误消息
- 如何在 3D OpenGL 场景上绘制 SDL 2D 矩形?
- "Undefined symbols for architecture x86_64" 在 SDL 2 (macOS) 中
- 为什么 SDL 在 Mac 上比 Linux 慢得多?
- 在 OSX 上打开 SDL 窗口时不要打开终端窗口
- 关于 Vulkan 并使用 GLFW 抽象出 Surface 创建
- SDL + OpenGL ES 2.0 使用着色器显示纹理
- SDL GPU 为什么将两个图像分成两个单独的循环更快?
- 在 cmake 中找不到 SDL 库
- SDL/C++: 如何平滑但随机地移动矩形?
- 如何与 SDL 库链接?
- 为什么我的C++代码不呈现 SDL 矩形?
- Surface Pro平板电脑上的QML应用程序键盘行为
- 来自 SDL Surface 的 openGL 纹理