14

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?

wrongusername
  • 18,564
  • 40
  • 130
  • 214
  • 1
    Normally this happens when you forget a `;` somewhere or you use something without defining it. Are you sure your header file looks exactly like that? – RedX Aug 16 '11 at 08:27
  • 1
    Another possibility: Are you compiling with a C compiler rather than a C++ compiler (but a C compiler should have complained about not being able to find ``). Anyhow, your code as-is works for me. So I suspect that RedX nailed it: That you haven't shown us the code that causes the bug. – David Hammen Aug 16 '11 at 08:31
  • @RedX - yup that is the entire header file I have. I'll take a look at James's answer to see if that's the problem. – wrongusername Aug 16 '11 at 08:36
  • @David I'm pretty sure I'm using a C++ compiler. I see Qt calling g++. – wrongusername Aug 16 '11 at 08:48
  • 1
    This could also be caused if you are trying to declare a namespace within a class. Found out the hard way... – wizurd Jun 18 '14 at 19:48

5 Answers5

29

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.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
1

I got here from a Google search. In case anyone lands on this page due to the same error, you can also get this error if you attempt to define a namespace alias at class scope.

Example:

namespace SomeLongNamespaceName { }
class SomeClass {
    namespace X = SomeLongNamespaceName; // This is illegal and will give the
                                         // compiler error from this question
};

More information is available here: C++ namespace alias in entire class scope

statueuphemism
  • 644
  • 2
  • 5
  • 13
1

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.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • It is included in 3 different files. The compiler is only complaining `In file included from filepath/vocabword.cpp:1:`, but for every file that uses `ui.h`, the include is the very first line of the file, so that cannot possibly be it. Any other ideas? – wrongusername Aug 16 '11 at 08:52
  • 1
    Provide a complete example showing the error. Start by stripping down your code until you've got something small enough to post. Chances are good that you in the process will discover your error yourself ;) – Kleist Aug 16 '11 at 09:12
  • maybe some different compiler settings for that particular .cpp? – imre Aug 16 '11 at 09:25
  • @wrongusername If the compiler is only complaining for one of the files that includes `ui.h`, then the problem is obviously in that file. (Maybe some foreign character which your editor doesn't display?) Otherwise: could you have a file named `string` somewhere in your include path, before the system `string`? – James Kanze Aug 16 '11 at 10:03
1

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)

WaffleSouffle
  • 3,293
  • 2
  • 28
  • 27
  • Thanks for the answer Waffle! Unfortunately using pragma once gives the same result, and I renamed UI_H to USERINTERFACE_H in case Qt already defined UI_H. No help. I've commented out the file Qt complained about, Qt just started to complain about another file. Will try to further hunt this down. – wrongusername Aug 16 '11 at 20:18
-1

"name" in my case was an error. Expected: unqualified-id before ‘namespace’ namespace inside a class declaration in one of the headers on top of the file. So I need to add extra semicolons after the final '}'. Thus such as '};;;;' will do.

ZF007
  • 3,708
  • 8
  • 29
  • 48