C++ C2039:类不是命名空间的成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30819201/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:49:11  来源:igfitidea点击:

C2039: Class is not a member of Namespace

c++

提问by Jesse Brands

Mage/Interface/Context.h

法师/界面/Context.h

#pragma once

#include <Mage/Interface/Element.h>
#include <Mage/Renderer/RenderingContext.h>
#include <Mage/Renderer/VertexBuffer.h>

#include <glm/glm.hpp>

namespace Mage {
    namespace Interface {
        class Context {

        protected:
            RenderingContext* ctx;
            VertexBuffer* vbo;
            glm::mat4 projection;
            Mage::Interface::Frame* uiParent;

        public:
            Context(RenderingContext* ctx);
            ~Context();

            void render();
            Mage::Interface::Frame* createFrame();
        };
    }
}

Mage/Interface/Element.h

法师/界面/Element.h

#pragma once

#include <vector>

#include <Mage/Interface/Context.h>

#include <glm/glm.hpp>

namespace Mage {
    namespace Interface {
        class Element {

        protected:
            Mage::Interface::Context* ctx;
            std::vector<Element*> children;
            glm::vec3 position;
            float scale;

        public:
            virtual void draw();

            void attach(Element* child) {
                this->children.push_back(child);
            }

            inline glm::vec3 getPosition() {
                return this->position;
            }

            float getScale() {
                return this->scale;
            }
        };

        // Frame is an untextured, single colour quad. Frame may contain other
        // Elements.
        class Frame : public Element {

        public:
            Frame();
            Frame(glm::vec3 pos);
            Frame(float width, float height);
            Frame(glm::vec3 pos, float width, float height);
        };
    }
}

This gives me the following errors:

这给了我以下错误:

Error   C2039   'Context': is not a member of 'Mage::Interface' Mage2D  c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h    14

Error   C2238   unexpected token(s) preceding ';'   Mage2D  c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h    14

Error   C2143   syntax error: missing ';' before '*'    Mage2D  c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h    14

Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    Mage2D  c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h    14

When I take out Mage::Interface::Context* ctx, the code compiles fine. I figured I must have missed a semi colon, but I can't see it - it all seems to check out just fine to me.

当我取出时Mage::Interface::Context* ctx,代码编译得很好。我想我一定漏掉了一个分号,但我看不到它 - 这一切对我来说似乎都很好。

回答by tux3

You have a circular dependency. Element.h includes Context.h and Context.h includes Element.h, that's not going to work.

你有一个循环依赖。Element.h 包括 Context.h 和 Context.h 包括 Element.h,那是行不通的。

The way to solve that is to forward-declare types instead of including their headers whenever you can, it'll also reduce compile times.

解决这个问题的方法是前向声明类型,而不是尽可能包含它们的头文件,它还可以减少编译时间。