c++ - 无法将字符串传递给我的类函数

标签 c++ sdl

我有一个具有此功能的类:

void Render(SDL_Surface *source,SDL_Surface *destination,string img)
{
    SDL_Rect offset;
    offset.x = m_x;
    offset.y = m_y;
    source = IMG_Load(img);
    offset.w = source->w;
    offset.h = source->h;    
}

出于某种原因,即使使用 include <string>在头文件的顶部它不会允许它。我得到:

Identifier, "string" is undefined.

我在我的主文件中传递这样的数据:

btn_quit.Render(menu,screen,"button.png");

当我执行时,我得到:

'Button::Render' : function does not take 3 arguments

但是这个网站说string是数据类型的正确语法(在底部):http://www.cplusplus.com/doc/tutorial/variables/

谁能解释一下我做错了什么?

最佳答案

我可能会建议您将 Render 函数更改为以下:

void Render(SDL_Surface *source,SDL_Surface *destination,const std::string& img)
{
    SDL_Rect offset;
    offset.x = m_x;
    offset.y = m_y;
    source = IMG_Load(img.c_str());
    offset.w = source->w;
    offset.h = source->h;    
}
  1. 使用 std::string 代替字符串
  2. 传递 img 引用而不是按值传递
  3. 从 IMG_Load(img) 更改;到 IMG_Load(img.c_str());

关于c++ - 无法将字符串传递给我的类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13216393/

相关文章:

c - SDL (C) - 如果没有事件循环,窗口将不会出现

c++ - VS 2008 QuickWatch 选项

c++ - 状态设计模式 : Error Handling

c++ - SHGetKnownFolderItem - 在 Wow64 上失败

c++ - Qt添加 "action"到动态添加的QWidget

c++ - SDL libsdl1.2-dev

c++ - SDL_RenderReadPixels 返回一个黑色矩形?

linux - glXSwapIntervalSGI 段错误

c++ - MVS 中的 SDL 奇怪链接器错误

c++ - 减法或递减指向开始的随机访问迭代器