无法覆盖抽象函数CreateFont

时间:2011-06-23 13:09:53

标签: visual-studio-2010 c++11

我有一个基类,我在其中放了一个抽象函数。现在我继承了它并在我的派生类中声明了该函数。但Visual Studio仍然不会编译我的代码并坚持派生类是抽象的。我甚至复制并粘贴了基类中的声明,只是删除了virtual=0,但仍然出现错误。我已经明确限定了所有参数并从全局范围返回值。有什么建议吗?

#include "OS/Windows/Windows.h"

#ifdef _DEBUG
#define D3D_DEBUG_INFO
#define D3DCALL(a) Wide::Render::CheckResult(a, #a)
#else
#define D3DCALL(a) a
#endif
#define D3DXFX_LARGEADDRESS_HANDLE

extern "C" {
    Wide::Render::Render* CreateRender(Wide::OSystem::Windows::WindowsOS* ptr);
}

namespace Wide {
    namespace Render {
        class D3D9Render : public Wide::Render::Render {
            friend Render* ::CreateRender(Wide::OSystem::Windows::WindowsOS*);
            D3D9Render(Wide::OSystem::Windows::WindowsOS* os);

            Wide::Colour ScreenBackground;

        public:
            Wide::Colour GetScreenBackground();
            Render* SetScreenBackground(Wide::Colour);
            void RenderFrame();

            int GetFramesPerSecond();
            std::shared_ptr<Wide::Render::Font> GetDefaultFont();
            std::shared_ptr<Wide::Render::Font> CreateFont(
                string name, 
                int weight,
                int width, 
                int height,
                bool italic
            );
        };
    }
}

Windows标题包含Render标题,其相关摘录发布在下面:

namespace Wide {
    namespace Render {
        struct Font;
        struct Label {
            virtual ::Wide::string Text() = 0;
            virtual Label* Text(::Wide::string) = 0; 

            virtual std::shared_ptr<::Wide::Render::Font> Font() = 0;
            virtual Label* Font(std::shared_ptr<::Wide::Render::Font>) = 0;

            virtual Label* Position(::Wide::Rectangle) = 0;
            virtual ::Wide::Rectangle Position() = 0;

            virtual ::Wide::Colour Colour() = 0;
            virtual Label* Colour(::Wide::Colour) = 0;

            virtual ~Label() {}
        };
        struct Font {
            virtual ::Wide::string Name() const = 0;
            virtual bool Italic() const = 0;
            virtual int Weight() const = 0;
            virtual int Width() const = 0;
            virtual int Height() const = 0;
            virtual std::unique_ptr<::Wide::Render::Label> CreateLabel() = 0;

            virtual ~Font() {}
        };
        struct Texture;
        struct Sprite {        
            virtual std::shared_ptr<::Wide::Render::Texture> Texture() = 0;
            virtual Sprite* Texture(std::shared_ptr<::Wide::Render::Texture>) = 0;

            virtual Sprite* Position(::Wide::Rectangle) = 0;
            virtual ::Wide::Rectangle Position() = 0;

            virtual ::Wide::Colour Colour() = 0;
            virtual Sprite* Colour(::Wide::Colour) = 0;

            virtual float Depth() = 0;
            virtual Sprite* Depth(float) = 0;

            virtual ~Sprite() {}
        };
        struct Texture {
            virtual ::Wide::Point Size() = 0;
            virtual ::Wide::string Filepath() = 0;

            virtual std::unique_ptr<Sprite> CreateSprite() = 0;
            virtual ~Texture() {}
        };
        struct Render {
            virtual ::Wide::Colour GetScreenBackground() = 0;
            virtual Render* SetScreenBackground(::Wide::Colour) = 0;
            virtual void RenderFrame() = 0;
            virtual int GetFramesPerSecond() = 0;

            virtual std::shared_ptr<Wide::Render::Font> GetDefaultFont() = 0;
            virtual std::shared_ptr<Wide::Render::Font> CreateFont(
                string name, 
                int weight,
                int width, 
                int height,
                bool italic
            ) = 0;

            virtual ~Render() {}
        };
    }
}

编辑:错误非常明确。

d:\backups\code\projects\ngin4\d3d9render\d3d9render.cpp(9): error C2259: 'Wide::Render::D3D9Render' : cannot instantiate abstract class
          due to following members:
          'std::tr1::shared_ptr<_Ty> Wide::Render::Render::CreateFont(Wide::string,int,int,int,bool)' : is abstract
          with
          [
              _Ty=Wide::Render::Font
          ]
          d:\backups\code\projects\ngin4\ngin4\render.h(107) : see declaration of 'Wide::Render::Render::CreateFont'

1 个答案:

答案 0 :(得分:3)

windows.h将CreateFont定义为CreateFontW(以及类似方式的许多其他标识符),导致一些包含顺序出现许多令人惊讶的编译错误:

#define CreateFont CreateFontW

常见的解决方法(假设您不需要调用此GDI函数)是在包含windows.h之后取消定义符号,或者完全避免使用此类Win32 API的名称。

#undef CreateFont