我有一个具有此功能的类:
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;
}
- 使用 std::string 代替字符串
- 传递 img 引用而不是按值传递
- 从 IMG_Load(img) 更改;到 IMG_Load(img.c_str());
关于c++ - 无法将字符串传递给我的类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13216393/