C ++类包含问题"没有在命名空间中命名为xxx"

时间:2017-04-02 14:58:54

标签: c++ namespaces

以下是代码 - 首先是组件类:

// LoSComponent.h

namespace LoS {
    class Component: public Ref {

    public:
        Component();
        ~Component();
    };
}

另一个类需要一个LoS :: Component指针作为参数:

LoSStrategyPreProcessBase.h

#include "LoSComponent.h"

namespace LoS {

    namespace Strategy {

        namespace PreProcess {

            class Base {
            public:
                Base(LoS::Component* losComponent)
                {
                    this->_losComponent = losComponent;
                };
                ~Base()
                {
                };
            public:
                LoS::Component* _losComponent;
            };
        }
    }
}

上面的代码会给我错误:

LoSStrategyPreProcessBase.h No type named 'Component' in namespace 'LoS'

有关如何将此代码编写为编译器的任何想法?

提前致谢。

更新1:

我试图将LoS :: Component替换为:: LoSComponent,但仍然遇到同样的错误:

enter image description here

更新2:

我用2个简单的类创建了一个简单的项目,我发现问题似乎是在包含头文件时引起的。

代码编译器&&链接确定:

LoSComponent.h

#ifndef LoSComponent_h
#define LoSComponent_h

namespace LoS {

    class Component {

    public:
        Component();
        ~Component();
    };
}


#endif /* LoSComponent_h */ 

并且:

#ifndef LoSBase_h
#define LoSBase_h

#include "LoSComponent.h"

namespace LoS {

    namespace Stragergy {
        class Base {

        public:
            Base(LoS::Component* los){
                this->_los = los;
            };
            ~Base();
        private:
            LoS::Component* _los;
        };

    }
}

#endif /* LoSBase_h */

如您所见,我可以在LoSBase.h中使用LoS :: Component而没有错误。

然后我尝试更改LoSComponent.h:

#ifndef LoSComponent_h
#define LoSComponent_h

#include "LoSBase.h"

namespace LoS {

    class Component {

    public:
        Component();
        ~Component();

    private:
        LoS::Stragergy::Base* _base;
    };
}


#endif /* LoSComponent_h */

如你所见,我添加了一个 #include "LoSBase.h"

LoS::Stragergy::Base* _base;

这次失败并出现错误:

LoSComponent.h:23:14: error: no member named 'Stragergy' in namespace 'LoS'
        LoS::Stragergy::Base* _base;
        ~~~~~^


LoSBase.h:20:23: error: no type named 'Component' in namespace 'LoS'
            Base(LoS::Component* los){
                 ~~~~~^

1 个答案:

答案 0 :(得分:0)

Base(LoS::Component* losComponent)更改为Base(::LoS::Component* losComponent)

命名空间,类或函数之前的

::告诉编译器在全局命名空间中查找命名空间/类/函数