C++命名空间的继承问题

发布于 2024-10-02 06:54:58 字数 2010 浏览 4 评论 0原文

好的,我一直在寻找,但无法为我的智慧找到为什么这不起作用的原因:

基类(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

派生类(图形) /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_resolutionY; }
                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

...这看起来相当微不足道。

派生类实现(graphics/handler.cpp):

#include "handler.h"

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

}

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

...这也应该是非常微不足道的,但会产生错误:

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

我正在将 MinGW 与代码块以及 CB 使用的标准设置一起使用,我尝试使用测试类构建相同的情况,并且在相同的环境和使用 vanilla g++ 的 Linux 中按预期工作。

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_resolutionY; }
                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++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-10-09 06:54:58

我在代码中看不到 t::misc::interface::Handler::Handler() 的任何实现 - 并且它将由继承类的构造函数调用,因此需要一个实现。链接器找不到它,所以它会抱怨。

: 更改

Handler();
virtual ~Handler();

只需将抽象类中的

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

为:就可以开始了。

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.

极致的悲 2024-10-09 06:54:58

顺便说一句,以两个下划线开头的标识符在 C++ 中是非法的(因为它们是为编译器保留的)。实际上,它们不应该成为预处理器中的问题,但为了安全起见,最好不要使用它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文