5

So, I'm fairly new to C++ programming but I have used SDL extensively with python and FreeBASIC. I'm sure I'm missing something silly here, but no matter what I try I keep getting the error "error: expected initializer before ‘namespace’" in my video.h file. It's driving me a little crazy.

#include "SDL/SDL.h"
#include <iostream>

namespace video {
// This is here because like video, everything uses it and the players should never be  able to touch it.
int rolldice(int minimumroll, int maximumroll, int numberofdice);
// Same Here.
char* charraystring(std::string prestring);
// Now we're in video proper
// This function loads an image, checks to make sure it works, returns the image, and unloads the testing surface.
SDL_Surface* loadimage(std::string path);
// This is an optimized blitter that will exit with a signal if it encounters an error.
void oblit(SDL_Surface* pic, SDL_Rect frame, SDL_Surface* screen, SDL_Rect location);
}
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Jsmith
  • 51
  • 1
  • 1
  • 5
  • Line 4. The actual error message is /home/dyngar/Workspace/C/CLAIR/video.h:4:1: error: expected initializer before ‘namespace’ Sorry, I copied the error from the old version of the file. – Jsmith Feb 01 '12 at 21:17
  • Are you sure that's your entire file? The line numbers in that error don't match up – Seth Carnegie Feb 01 '12 at 21:18
  • Sorry, I pasted the error message from the earlier version of the file. It's actually on line 4. Dang you guys are fast. Thanks in advance. – Jsmith Feb 01 '12 at 21:19
  • can you post the beginning of the file in which you include video.h and the complete output from the compiler? – Johan Lundberg Feb 01 '12 at 21:20
  • if you run your source file through the preprocessor (e.g. `gcc -I /usr/include -I /usr/include/c++/* -E source.cc`), what's on the line before "namespace video {"? – Managu Feb 01 '12 at 21:40
  • @Jonah Lunberg -the only file that uses video.h is called entities.cpp #include "entities.h" #include "SDL/SDL.h" #include "video.h" #include #include #include int main( int argc, char* args[] ) { } the line before namespace video is just int main( int argc, char* args[] ); – Jsmith Feb 01 '12 at 21:48
  • @Managu actually, the line before is # 3 "video.h" 2 – Jsmith Feb 01 '12 at 21:56
  • @Jsmith: Errm, how 'bout a line that doesn't start with `#`? I.e. a line of code that's not just a preprocessor directive. So ignore blank lines, too. Maybe I should ask "what's the previous complete syntactic construct?" Complete function declaration/class declaration/variable declaration/etc. – Managu Feb 01 '12 at 23:10
  • Have this code build on an earlier ubuntu version like 16.04lts ? I had similar issues when porting code that was running on 16.04lts and should now run on 18.04lts, however I have not yet figured out why this issue is there – serup Oct 25 '20 at 09:07

1 Answers1

14

The error you offer, error: expected initializer before ‘namespace’ suggests that there is a structure or variable declaration that isn't terminated. Something like:

struct foo {
    ...
}

namespace video {
    ...

Here, the 'struct foo' declaration isn't terminated with a semicolon. This should read:

struct foo {
    ...
};

namespace video {
    ...

Getting the preprocessor involved (using #include) makes this type of thing a bit harder to track down. It may be that you include a header (just before making the namespace video declaration) that doesn't terminate a structure definition, for example.

Go and check that all of your structs and classes have a semicolon after the closing curly brace in your headers and source files. Similarly any variable declarations, e.g.

int value // <-- oops, forgot the ';'

namespace video {
    ...
Managu
  • 8,849
  • 2
  • 30
  • 36
  • That's what I figured at first too, but I couldn't find one. I'm going to go through the SDL/SDL.h file to see what it has. I don't know if it's relevant, but when I try to compile using g++ -I /home/dyngar/Workspace/C/CLAIR -std=gnu++0x video.cpp -o video.o -lSDL it works just fine. Which seems really weird, but like I said, I'm new to c++. – Jsmith Feb 01 '12 at 21:43