Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C2039: Class is not a member of Namespace

Tags:

c++

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 figured I must have missed a semi colon, but I can't see it - it all seems to check out just fine to me.

like image 958
Jesse Brands Avatar asked Nov 28 '22 14:11

Jesse Brands


1 Answers

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

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

like image 104
tux3 Avatar answered Dec 01 '22 05:12

tux3