Msgstr "Error of expected invalid identifier before name".

I have the following seemingly harmless 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?

+6
source share
3 answers

One way to track such errors is to start from scratch:

 #include "filepath/ui.h" int main () { return 0; } 

Will it compile? (This works fine with the small ui.h snippet you provided.)

Errors like these are often caused by the absence of a semicolon in the previous class declaration. So try to get the problem:

 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. I get an intricate trace of the path from my testmain.cpp to the /ui.h file path to the line ... and I end up with

 /usr/include/i386/_types.h:37: error: two or more data types in declaration of '__int8_t' 

So this is not a mistake, but the missing semicolon is the creation of a mess. Your error does not occur deep within the <string> , so let's do 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 an error message

 In file included from testmain.cpp:5: filepath/ui.h:6: error: expected unqualified-id before 'namespace' 

And here it is. Thus, some other heading that you #include before filepath / ui.h has a poorly formed class declaration.

Adding
Sometimes this helps to use a different compiler. g ++ is known for its poor handling of this common programming error. Compiling above with clang tutorials

 testmain.cpp:4:2: error: expected ';' after struct 

So tada, clang resets the problem.

What happens is that when the compiler encounters a problem, it applies some fix to your code to make it grammatically correct. The compiler error message is based on this auto-correction. Remember well: this auto-correction as a whole is a very good thing. Without it, the compiler would have to shut down on the first error. Since programmers inevitably make more than one mistake, hunting for them one by one will be a pain in the rear.

I don't have the faintest idea which stupid g ++ correction is used to fix a missing semicolon problem, except that it doesn't add an obvious semicolon. clang adds the missing semicolon, and he complains about it.

+16
source

Where does this file come from. There is nothing wrong with the file you sent; what I suspect is that the file that it includes already includes <string> (so that it does nothing) and is missing ; just before including your file.

+1
source

Well, that is weird. Is there anything else #defined UI_H (which shouldn't cause a problem, but who knows), or ui?

Does the same C # pragma happen once (if your compiler supports it)?

Will you literally pair with the file so that all other code is commented out?

(apologies for sending more questions, not answers)

+1
source

Source: https://habr.com/ru/post/895124/


All Articles