Problem with 'string'

The following program copied is not compiling in Anjutha with “/home/jayakumar/sdl-foobar/src/main.c:13: error: expected ?)? before ?filename?” message. The file name is main.c .Please help.

#include “SDL/SDL.h”
#include <string.h>

const int SCREEN_HEIGHT = 480;
const int SCREEN_WIDTH = 640;
const int SCREEN_BPP = 32;

SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *loadimage(string filename){
SDL_Surface *loadedimage = NULL;
SDL_Surface *optimizedimage = NULL;

loadedimage    = SDL_LoadBMP(filename.c_str());
optimizedimage = SDL_DisplayFormat(loadedimage);
SDL_FreeSurface(loadedimage);
return optimizedimage;

}

void applysurface(int x,int y,SDL_Surface *source,SDL_Surface *destination){
SDL_Rect offset;
offset.x=x;
offset.y=y;
SDL_BlitSurface(source,NULL,destination,&offset);
}

int main(int argc,char* args[]){
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(SCREEN_HEIGHT,SCREEN_WIDTH,SCREEN_BPP,SDL_SWSURFACE);
message = SDL_LoadBMP(“hello.bmp”);
background = SDL_LoadBMP(“background.bmp”);
applysurface(0,0,background,screen);
applysurface(320,0,background,screen);
applysurface(0,240,background,screen);
applysurface(320,240,background,screen);
applysurface(160,140,message,screen);
DSL_FreeSurface(background);
DSL_FreeSurface(message);
SDL_Flip(screen);
SDL_Delay(2000);
SDL_Quit();
return 0;
}

Thanks in advance.

This is a general programming question, not SDL specific. I would hint
though that you appear to be missing some include directives.
std::string is a C++ feature, you should use main.cpp or manually tell
your compiler to treat the code as C++. You will need to either have
std::string as the parameter type, or have a “using” directive, such
as “using std::string;” or “using namespace std;” after your includes
to resolve the name. Your code is lacking in error checking, all SDL
functions might return a value indicating an error. Your code should
test for these conditions and react appropriately (e.g. warn the user
a file could not be loaded, or simply quit the program for the
moment).

Finally, you will probably want to use your loadimage() function
inside main given that you’ve gone to the trouble of writing it.
Currently you are just using SDL_LoadBMP.

If you have any further queries, I recommend you go to a mailing list
or web forum which will help you with your programming questions, for
example www.gamedev.net.

Hope this helps,

– Brian

2010/1/10 wheelchairman :> The following program copied is not compiling in Anjutha with

“/home/jayakumar/sdl-foobar/src/main.c:13: error: expected ?)? before
?filename?” message. The file name is main.c .Please help.

#include “SDL/SDL.h”
#include

const int SCREEN_HEIGHT = 480;
const int SCREEN_WIDTH = 640;
const int SCREEN_BPP = 32;

SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *loadimage(string filename){
SDL_Surface *loadedimage = NULL;
SDL_Surface *optimizedimage = NULL;

loadedimage = SDL_LoadBMP(filename.c_str());
optimizedimage = SDL_DisplayFormat(loadedimage);
SDL_FreeSurface(loadedimage);
return optimizedimage;
}

void applysurface(int x,int y,SDL_Surface *source,SDL_Surface *destination){
SDL_Rect offset;
offset.x=x;
offset.y=y;
SDL_BlitSurface(source,NULL,destination,&offset);
}

int main(int argc,char* args[]){
SDL_Init(SDL_INIT_EVERYTHING);
screen =
SDL_SetVideoMode(SCREEN_HEIGHT,SCREEN_WIDTH,SCREEN_BPP,SDL_SWSURFACE);
message = SDL_LoadBMP(“hello.bmp”);
background = SDL_LoadBMP(“background.bmp”);
applysurface(0,0,background,screen);
applysurface(320,0,background,screen);
applysurface(0,240,background,screen);
applysurface(320,240,background,screen);
applysurface(160,140,message,screen);
DSL_FreeSurface(background);
DSL_FreeSurface(message);
SDL_Flip(screen);
SDL_Delay(2000);
SDL_Quit();
return 0;
}

Thanks in advance.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

it looks like you try to use a c++ feature in c.
c has no string class nor a string datatype.
c uses character arrays.
so your function should look like
SDL_Surface *loadimage(char *filename)
maybe read K&R "The C Programming language"
i see some other errors like typos tooOn 01/10/2010 01:36 PM, wheelchairman wrote:

The following program copied is not compiling in Anjutha with
"/home/jayakumar/sdl-foobar/src/main.c:13: error: expected ‘)’ before
’filename’" message. The file name is main.c .Please help.

#include “SDL/SDL.h”
#include

const int SCREEN_HEIGHT = 480;
const int SCREEN_WIDTH = 640;
const int SCREEN_BPP = 32;

SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *loadimage(string filename){
SDL_Surface *loadedimage = NULL;
SDL_Surface *optimizedimage = NULL;

loadedimage = SDL_LoadBMP(filename.c_str());
optimizedimage = SDL_DisplayFormat(loadedimage);
SDL_FreeSurface(loadedimage);
return optimizedimage;
}

void applysurface(int x,int y,SDL_Surface *source,SDL_Surface
*destination){
SDL_Rect offset;
offset.x=x;
offset.y=y;
SDL_BlitSurface(source,NULL,destination,&offset);
}

int main(int argc,char* args[]){
SDL_Init(SDL_INIT_EVERYTHING);
screen =
SDL_SetVideoMode(SCREEN_HEIGHT,SCREEN_WIDTH,SCREEN_BPP,SDL_SWSURFACE);
message = SDL_LoadBMP(“hello.bmp”);
background = SDL_LoadBMP(“background.bmp”);
applysurface(0,0,background,screen);
applysurface(320,0,background,screen);
applysurface(0,240,background,screen);
applysurface(320,240,background,screen);
applysurface(160,140,message,screen);
DSL_FreeSurface(background);
DSL_FreeSurface(message);
SDL_Flip(screen);
SDL_Delay(2000);
SDL_Quit();
return 0;
}

Thanks in advance.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org