开发者

C++ inheritance problem with namespaces

OK, I have been looking about but can not for the wits of me find a reason to why this should not work:

Base class (misc/interface/handler.h)

#ifndef __t__MISC_VIRTUAL_HANDLER_H
#define __t__MISC_VIRTUAL_HANDLER_H
#pragma message("Starting with 'handler.h'")

namespace t {
    namespace misc {
        namespace interface {
            class Handler {
                public:
                    Handler();
                    virtual ~Handler();

                    virtual int setup() = 0;
                    virtual int teardown() = 0;
                    virtual int update() = 0;
                protected:
                private:
            };
        }
    }
}

#pragma message("Ending with 'handler.h'")
#endif // __t__MISC_VIRTUAL_HANDLER_H

Derived class (graphics/handler.h):

#ifndef __t_GRAPHICS_HANDLER_H
#define __t_GRAPHICS_HANDLER_H

#include "../misc/interface/handler.h"

namespace t {
    namespace graphics {
        class Handler: public t::misc::interface::Handler {
            public:
                Handler();
                virtual ~Handler();

                int getResolutionX() { return m_resolutionX; }
                int getResolutionY() { return m_resolutio开发者_开发知识库nY; }
                bool getFullscreen() { return m_isFullscreen; }

            protected:
            private:
                unsigned int m_resolutionX, m_resolutionY;
                bool m_isFullscreen;

        }; // class Handler
    } // namespace graphics
} // namespace t
#endif // __t_GRAPHICS_HANDLER_H

... which seems rather trivial.

Derived class implementation (graphics/handler.cpp):

#include "handler.h"

t::graphics::Handler::Handler(): t::misc::interface::Handler() {

}

t::graphics::Handler::~Handler() {
}

... which too is should be really trivial, but yields the error:

src\graphics\handler.cpp|5|undefined reference to `t::misc::interface::Handler::Handler()'

I'm using MinGW with Code Blocks and what ever standard settings CB uses, I've tried building the same situation with test classes and that works as intended, both in same environment and Linux with vanilla g++.


I can't see any implementation of t::misc::interface::Handler::Handler() in your code - and it is going to be called by the inheriting class's constructor, so it needs an implementation. The linker can't find it, so it complains.

Just change:

Handler();
virtual ~Handler();

in the abstract class to:

Handler() {}
virtual ~Handler() {}

and you're ready to go.


As an aside, identifiers starting with two underscores are illegal in C++ (since they are reserved for the compiler). In practice, they shouldn’t be a problem in preprocessor but it’s best to err on the safe side here: simply don’t use them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜