C2039: Class is not a member of the namespace

Mage / Interface / 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

#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 thought I must have missed partial shade, but I can't see it - it all seems to work great for me.

+3


source to share


1 answer


You have a circular dependency. Element.h includes Context.h and Context.h, includes Element.h, which won't work.



The way around this problem is to forward type declarations rather than include their headers whenever possible, this also saves compilation time.

+15


source







All Articles