C++ “'命名空间'之前的预期不合格ID”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7075429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Expected unqualified-id before 'namespace'" error
提问by wrongusername
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?
我在这里做错了什么?
回答by David Hammen
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.)
这能编译吗?(这适用于您提供的 ui.h 小片段。)
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
这当然不会编译干净。我从我的 testmain.cpp 到你的文件路径/ui.h 得到一个复杂的包含路径跟踪到字符串......并最终得到
/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:
所以这不是错误,但缺少的分号肯定会造成混乱。你的错误不是在 的深处产生<string>
,所以让我们#include <string>
在尝试重新创建错误之前制作我们的测试程序:
#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.
就在那里。所以你在 filepath/ui.h 之前 #include 的一些其他头文件有一个格式错误的类声明。
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
附录
有时使用不同的编译器会有所帮助。g++ 因其对这种常见编程错误的不良处理而臭名昭著。用 clang 产量编译以上内容
testmain.cpp:4:2: error: expected ';' after struct
So, tada, clang has zeroed in on the problem.
所以,tada,clang 已经将注意力集中在了这个问题上。
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.
除了不添加明显丢失的分号之外,我还不清楚 g++ 应用什么愚蠢的修正来解决缺少分号的问题。clang 添加了缺少的分号,这就是它所抱怨的。
回答by James Kanze
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.
这个文件是从哪里包含的。您发布的文件没有任何问题;我怀疑正在发生的是包含它的文件已经包含<string>
(所以这个包含什么都不做),并且;
在它包含您的文件之前缺少一个。
回答by WaffleSouffle
Well, that's weird. Has anything else #defined UI_H (which shouldn't cause a problem but who knows), or ui ?
嗯,这很奇怪。还有其他 #defined UI_H (这不应该引起问题,但谁知道)或 ui ?
Does the same thing happen with a #pragma once (assuming your compiler supports it) ?
#pragma once 是否会发生同样的事情(假设您的编译器支持它)?
Have you literally paired down the file so that all the other code is commented out ?
您是否真的将文件配对,以便注释掉所有其他代码?
(apologies for posting more questions rather than answers)
(抱歉发布更多问题而不是答案)
回答by statueuphemism
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
此处提供更多信息: C++ 命名空间别名在整个类范围内