命名空间被视为类型

时间:2015-10-10 14:26:56

标签: c++ c++11 namespaces cmake

我有两个文件Collider.h和Collidable.h,以这种方式组织:

Folder 1:
----Collider.h
----Folder 2:
--------Collidable.h

这里是文件内容: Collidable.h

#pragma once


#include "../Collider.h"

namespace nsInterfaces
{
    class Collidable
    {

    public:
        nsEngine::Collider * col {nullptr};
    public:
        virtual bool collides (const Collidable & other) const noexcept = 0;
        virtual ~Collidable () { delete col; }

    };
}



typedef std::unique_ptr<nsInterfaces::Collidable>   uptCollidable;
typedef std::shared_ptr<nsInterfaces::Collidable>   sptCollidable;
typedef std::vector<sptCollidable>                  CollidableVector;

Collider.h

#pragma once

#include "../Utility/Typedef.hpp"
#include "../Utility/GameException.h"

#include "Movement.h"

namespace nsEngine
{
    class Collider
    {
    /**/
    private:
    public:

        Collider () {}
        virtual ~Collider () noexcept {}

        virtual Collider * clone () noexcept = 0;
    protected:
        float x;
        float y;
        /**/
    };
}

我正在为自己开发一款游戏,正如您所料,但我仍然会遇到同样的错误:

error: 'nsEngine' does not name a type
     nsEngine::Collider * col {nullptr};
     ^

当然还有error: "col" was not declared in this scope

我正在使用cmake和mingw w64(我相信GCC 4.9.1)。我知道我可能没有提供足够的信息,所以请问我!

感谢您的阅读。

3 个答案:

答案 0 :(得分:0)

我试图在我的环境(Windows / Visual studio)下重现您的问题,我没有这个问题。我唯一的错误是Impossible to open the source file ../Collider.h,如果它不在构建路径中(即使collidable.h是),并且在这种情况下会出现相同的'ncEngine' does not name a type错误。

我注意到您使用当前文件中的相对路径包含所有文件。例如:../Utility/GameException.h ...为什么不宁愿使用与项目根目录相关的路径并包含这样的文件:

#include "Engine/collider.h"
#include "Engine/Interface/collidable.h"
#include "Utility/GameException.h"

这可能无法解决您的问题,但它确实在我的Win / Visual环境中,并且(在我看来)使代码更容易理解。

答案 1 :(得分:0)

我尝试使用MINGW和g ++ 5.1,我实际上可以重现错误,当我忘记在pragma once前放置#时,我得到了它。如果我把#放在前面就会正确构建,所以我看到它就是偶然的事。它似乎与包含相对路径无关,因此就像pragma曾经不起作用。

我建议尝试使用#ifndef/#define/#endif警卫,而不是使用#pragma once.

答案 2 :(得分:0)

这是循环包含的错误问题,绕过#pragma once和/或#ifndef/#define#endif

但是找不到原因。