Sir
Sir

Reputation: 8280

Can't pass a string to my class function

I have a class with this function:

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;    
}

For some reason though even with include <string> at the top of the header file it won't allow it. I get:

Identifier, "string" is undefined.

Im passing the data like this on my main file:

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

When i execute i get :

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

But this site says string is the correct syntax for the data type (at the bottom): http://www.cplusplus.com/doc/tutorial/variables/

Can some one explain what I am doing wrong ?

Upvotes: 0

Views: 1028

Answers (1)

billz
billz

Reputation: 45410

I may suggest you change Render function to below:

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. use std::string instead of string
  2. pass img reference instead of passing by value
  3. change from IMG_Load(img); to IMG_Load(img.c_str());

Upvotes: 3

Related Questions