I ran into a roadblock called THE STRING.

Hello, I am new to C++ and have ran into a road block; I hope you guyes would assist me in discovering the problem. Here is the code:

//The Header
#include "SDL/SDL.h"
#include <string>

//The sttributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces that will be used
SDL_Surface *Hello = NULL;
SDL_Surface *Background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *load_image(std::string filename)
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedimage = NULL;

//The optimized image that will be used
SDL_Surface* optimizedimage = NULL;

//Load the image
loadimage = SDL_LoadBMP(filename.c_str[]);

//If nothing went wrong in loading the image
if(loadedimage != NULL)
{
//Creat an optimized image
optimizedimage = SDL_DisplayFormat(loadedimage);

//Free the old image
SDL_FreeSurface(loadedimage);
}
//Return the optimized image
return optimizedimage;
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;

//Give the offsets to the rectangle
offset.x = x;
offset.y = y;

//Blit the surface
SDL_BlitSurface(source, NULL, destination, &offset);
}

int main(int argc, char* args[])
{
//Initialize all SDL subsystems
if(SDL_Init(SDL_INIT_EVERYTHING)==-1)
{
return 1;
}
//Set up the screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

//If there was error in setting up the screen
if(screen == NULL)
{
return 1;
}
//Set the window caption
SDL_WM_SetCaption("The Super Mario", NULL);

//Load the images
Hello = load_image("Hello.bmp");
Background = load_image("Background.bmp");

//Apply the background to the screen
apply_surface(0, 0, Background, screen);
apply_surface(320, 0, Background, screen);
apply_surface(0, 240, Background, screen);
apply_surface(320, 240, Background, screen);

//Apply the message to the screen
apply_surface(180, 140, Hello, screen);

//update the screen
if(SDL_Flip(screen)==-1)
{
return 1;
}
//Wait 2 seconds
SDL_Delay(2000);

//Free the surface
SDL_FreeSurface(Hello);
SDL_FreeSurface(Background);

//Quit SDL
SDL_Quit();

//Return
return 0;
}


I get an error for line 3 which is #include <string>
The error reads - Error: String: No such file or directory

And I get another error for line 15 which is SDL_Surface *load_image(std::string filename)
The error reads - Error:expected ')' before ':' token

I tried to be as detailed as possible so if any body can assist me on what I am doing wrong , I would gladly be thankful.
Last edited on
What compiler do you use? If it's too old it may not support std::string.

EDIT: Try CodeBlocks ( http://www.codeblocks.org/ )
Last edited on
You may want to check that you're using a C++ compiler, not a C compiler.
Yeah, what compiler?? I use GNU g++, and this is what happened when I tried to compile...

a.cpp:8:21: SDL/SDL.h: No such file or directory
a.cpp:17: error: expected constructor, destructor, or type conversion before '*' token
a.cpp:18: error: expected constructor, destructor, or type conversion before '*' token
a.cpp:19: error: expected constructor, destructor, or type conversion before '*' token
a.cpp:21: error: expected constructor, destructor, or type conversion before '*' token
a.cpp:43: error: `SDL_Surface' has not been declared
a.cpp:43: error: `SDL_Surface' has not been declared
a.cpp:43: error: ISO C++ forbids declaration of `source' with no type
a.cpp:43: error: ISO C++ forbids declaration of `destination' with no type
a.cpp: In function `void apply_surface(int, int, int*, int*)':
a.cpp:45: error: `SDL_Rect' was not declared in this scope
a.cpp:45: error: expected `;' before "offset"
a.cpp:48: error: `offset' was not declared in this scope
a.cpp:52: error: `SDL_BlitSurface' was not declared in this scope
a.cpp:45: warning: unused variable 'SDL_Rect'
a.cpp:52: warning: unused variable 'SDL_BlitSurface'
a.cpp: In function `int main(int, char**)':
a.cpp:57: error: `SDL_INIT_EVERYTHING' was not declared in this scope
a.cpp:57: error: `SDL_Init' was not declared in this scope
a.cpp:57: warning: unused variable 'SDL_INIT_EVERYTHING'
a.cpp:57: warning: unused variable 'SDL_Init'
a.cpp:61: error: `screen' was not declared in this scope
a.cpp:62: error: `SDL_SWSURFACE' was not declared in this scope
a.cpp:62: error: `SDL_SetVideoMode' was not declared in this scope
a.cpp:69: error: `SDL_WM_SetCaption' was not declared in this scope
a.cpp:72: error: `Hello' was not declared in this scope
a.cpp:72: error: `load_image' was not declared in this scope
a.cpp:73: error: `Background' was not declared in this scope
a.cpp:85: error: `SDL_Flip' was not declared in this scope
a.cpp:85: warning: unused variable 'SDL_Flip'
a.cpp:89: error: `SDL_Delay' was not declared in this scope
a.cpp:92: error: `SDL_FreeSurface' was not declared in this scope
a.cpp:96: error: `SDL_Quit' was not declared in this scope
a.cpp:62: warning: unused variable 'SDL_SWSURFACE'
a.cpp:62: warning: unused variable 'SDL_SetVideoMode'
a.cpp:69: warning: unused variable 'SDL_WM_SetCaption'
a.cpp:89: warning: unused variable 'SDL_Delay'
a.cpp:96: warning: unused variable 'SDL_Quit'


Also what OS do you use? I'm on windows right now, so that could have something to do with it.

And, what is SDL?
Last edited on
RyanCaywood wrote:
I use GNU g++, and this is what happened when I tried to compile...

Well, that's because you haven't installed SDL.

RyanCaywood wrote:
And, what is SDL?

http://en.wikipedia.org/wiki/Simple_DirectMedia_Layer
http://www.libsdl.org/
Last edited on
I use Codeblock 10.2 or 10.5
Don't use load_image, use SDL_LoadBMP instead. SDL_LoadBMP does not require std::string.
Hello = SDL_LoadBMP("hello.bmp");
Does that work?
Last edited on
I get an error for line 3 which is #include <string>
The error reads - Error: String: No such file or directory


Did you copy and paste the error message? Looking at it, I'm thinking you may have used #include <String> instead of #include <string> ...
Topic archived. No new replies allowed.