2
votes

Given the searches, I feel as though this sort of thing has been asked many times. Nevertheless, I couldn't find an answer so here goes:

My setup: CodeBlocks, GCC C++ and SDL
I'm currently going through LazyFoo's SDL tutorials, and am getting this error:

error: expected ')' before ':' token

from this code:

SDL_Surface* load_image(std::string myfile)

in this context

#include "SDL/SDL.h"

#include < string.h >

const int SCREEN_WIDTH = 425;
const int SCREEN_HEIGHT = 550;
const int SCREEN_BPP = 32;

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

SDL_Surface* load_image(std::string myfile)
{
more stuff and the rest of the program etc
}

I feel that this may have something to do with the way my string or SDL libraries are set up in CodeBlocks. Any suggestions or obvious links to solutions that I missed?

5
change "#include < string.h >" to "#include <string>"lsalamon

5 Answers

8
votes

Include <string> instead of < string.h > if you want to use the C++ STL strings library.

2
votes

You should use string instead of string.h in the inclusion from the header.

1
votes

I was also following the tutorial at: http://lazyfoo.net/SDL_tutorials/lesson02/index.php and got the message: line 3, error: string: No such file or directory line 15, error: expected ')' before ':' token

i think i found the reason for the problem.

When choosing "File / New / Empty file", it defaults to the file extension Untitled1.c , i changed the file extension to .cpp and that made Code Blocks recognize #include , and now the application works.

0
votes

That sounds like a C error; you should be compiling with g++, not gcc.

0
votes

I figured it out. I had the same problem but I got it working. Here is exactly what I did. First downloaded the sourcefiles that lazy foo gave at the end of the tutorial. I tried them and I got the error that SDL.dll was not found on computer. I moved the SDL.dll file from the folder the project was in which should have worked fine, to the C:/windows/system32.

Next, I tried the program again, the one that comes with the tutorial, the window flashed but with nothing showing, I copied in the two bpm files in to the project and it worked.

Definitely the file needs to be .cpp so I'll say that.

Hold on there's more. Now I thought for sure that my attempt at lesson2 had no errors, I typed everything exactly as I saw it. However that was the problem.

loadedImage = SDL_LoadBMP( filename.c_str() ); <__where he shows this, with the font he uses, it looks more like loadedImage = SDL_LoadBMP( filename.c_str0 );

Just look at it...it does, anwhere he types () will look like a 0 and you really have to know.

So anyone having this problem, as well as any other advice you get I suggest checking your code for the tricky () that you might have read and typed in as a 0.