简体   繁体   中英

How to fix error: unknown type name ‘namespace’

#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:

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?

It sounds like you're trying to compile your C++ code with a C compiler. Try using g++ instead of gcc and giving your file a C++ extension such as .cpp (rather than .c ).

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.

Had this issue with YCM and clang. Turns out, the missing flag was "-x", "c++" .

From the official clangdocumentation :

-x <language> : Treat subsequent input files as having type language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM