C++ 如何修复错误:未知类型名称“命名空间”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13602249/
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
How to fix error: unknown type name ‘namespace’
提问by Daniel
#ifndef UNO_ACTION_
#define UNO_ACTION_
namespace Uno
{
namespace Game
{
class Game;
}
} // namespace
namespace Uno
{
namespace Action
{
using ::Uno::Game::Game;
class Action
{
public:
virtual bool isDisposeable() = 0;
virtual void takeAction(Game* game) = 0;
virtual ~Action() {}
};
}
}
#endif
I compile these code on ubuntu 12.04 and it returns to set of error:
我在 ubuntu 12.04 上编译这些代码,它返回到一组错误:
action.h:4:1: error: unknown type name ‘namespace'
action.h:4:15: error: expected ‘=', ‘,', ‘;', ‘asm' or ‘__attribute__' before ‘{' token
action.h:8:1: error: unknown type name ‘namespace'
action.h:8:15: error: expected ‘=', ‘,', ‘;', ‘asm' or ‘__attribute__' before ‘{' token
How do I solve these errors?
我该如何解决这些错误?
回答by NPE
It sounds like you're trying to compile your C++ code with a C compiler. Trying using g++
instead of gcc
and giving your file a C++ extension such as .cpp
(rather than .c
).
听起来您正在尝试使用 C 编译器编译 C++ 代码。尝试使用g++
而不是gcc
为您的文件提供 C++ 扩展名,例如.cpp
(而不是.c
)。
回答by EwoutVDC
I had a similar issue and found this question but the solutions don't match mine completely, so I'm adding mine here.
我有一个类似的问题并发现了这个问题,但解决方案与我的不完全匹配,所以我在这里添加我的。
In my case, I was including a header file in .cpp files and .c files. The solution was to split off the namespace part of the header since that was obviously only needed in the .cpp files.
就我而言,我在 .cpp 文件和 .c 文件中包含了一个头文件。解决方案是拆分标题的命名空间部分,因为这显然只在 .cpp 文件中需要。