I have the following seemingly innocuous piece of code:
#ifndef UI_H
#define UI_H
#include <string>
namespace ui
{
//Displays the main menu, showing loaded vocabulary cards
//
//Returns upon completion of display
void displayMainMenu();
//...More code like the above, just comments followed by functions
}
#endif
which gives me this error message:
filepath/ui.h:6: error: expected unqualified-id before 'namespace'
What am I doing wrong here?
One way to track down such errors is to start from the ground up:
#include "filepath/ui.h"
int main () { return 0; }
Does this compile? (This works fine with the little snippet of ui.h that you supplied.)
Errors like these are often caused by a missing semicolon on some previous class declaration. So let's try to force the issue:
struct Foo { int foo; } // Note the missing semicolon after the close brace.
#include "filepath/ui.h"
int main () { return 0; }
This of course does not compile clean. I get a convoluted include path trace from my testmain.cpp to your filepath/ui.h to string ... and eventually get
/usr/include/i386/_types.h:37: error: two or more data types in declaration of '__int8_t'
So that isn't the error, but the missing semicolon sure is creating a mess. Your error isn't arising deep in the bowels of <string>
, so let's make our test program #include <string>
before trying to recreate the error:
#include <string>
struct Foo { int foo; } // Note the missing semicolon after the close brace.
#include "filepath/ui.h"
int main () { return 0; }
And the error message is
In file included from testmain.cpp:5:
filepath/ui.h:6: error: expected unqualified-id before 'namespace'
And there it is. So some other header that you #include prior to filepath/ui.h has a badly-formed class declaration.
Addendum
Sometimes it helps to use a different compiler. g++ is notorious for its bad treatment of this common programming error. Compiling the above with clang yields
testmain.cpp:4:2: error: expected ';' after struct
So, tada, clang has zeroed in on the problem.
What is happening is that when a compiler runs into trouble it applies some fix to your code to make it grammatically correct. The compiler error message is based on this autocorrection. Note well: This autocorrection is in general a very good thing. Without it the compiler would necessarily have to shut down at the first error. Since programmers inevitably make more than one error, hunting them down one at a time would be a pain in the rear.
I haven't the foggiest idea what goofy correction g++ applies to fix the missing semicolon problem, other than it is not to add the obvious missing semicolon. clang adds the missing semicolon, and that is what it complains about.
From where is this file included. There's nothing wrong with the file
you posted; what I suspect is happening is that the file which includes
it has already includes <string>
(so this include does nothing), and
is missing a ;
immediately before it includes your file.
Well, that's weird. Has anything else #defined UI_H (which shouldn't cause a problem but who knows), or ui ?
Does the same thing happen with a #pragma once (assuming your compiler supports it) ?
Have you literally paired down the file so that all the other code is commented out ?
(apologies for posting more questions rather than answers)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With