C ++命名空间和语法标题文件错误

时间:2012-07-14 03:43:42

标签: c++ header namespaces include

我在头文件中遇到常见错误,如下所示。

Error   54  error C3083: 'Resources': the symbol to the left of a '::' must be a type   e:\users\nkosi\documents\visual studio 2010\projects\directx games\direct2d\dx2dhelper\dx2dhelper\sprite.h  28  1   DX2DHelper
Error   55  error C2039: 'DX2DImage' : is not a member of 'DX2DHelper'  e:\users\nkosi\documents\visual studio 2010\projects\directx games\direct2d\dx2dhelper\dx2dhelper\sprite.h  28  1   DX2DHelper
Error   56  error C2061: syntax error : identifier 'DX2DImage'  e:\users\nkosi\documents\visual studio 2010\projects\directx games\direct2d\dx2dhelper\dx2dhelper\sprite.h  28  1   DX2DHelper
Error   57  error C3083: 'Resources': the symbol to the left of a '::' must be a type   e:\users\nkosi\documents\visual studio 2010\projects\directx games\direct2d\dx2dhelper\dx2dhelper\sprite.h  32  1   DX2DHelper
Error   58  error C2039: 'DX2DImage' : is not a member of 'DX2DHelper'  e:\users\nkosi\documents\visual studio 2010\projects\directx games\direct2d\dx2dhelper\dx2dhelper\sprite.h  32  1   DX2DHelper
Error   59  error C2143: syntax error : missing ';' before '*'  e:\users\nkosi\documents\visual studio 2010\projects\directx games\direct2d\dx2dhelper\dx2dhelper\sprite.h  32  1   DX2DHelper
Error   60  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   e:\users\nkosi\documents\visual studio 2010\projects\directx games\direct2d\dx2dhelper\dx2dhelper\sprite.h  32  1   DX2DHelper
Error   61  error C2061: syntax error : identifier 'GameBase'   e:\users\nkosi\documents\visual studio 2010\projects\directx games\direct2d\dx2dhelper\dx2dhelper\systemclass.h 30  1   DX2DHelper

然而,对于我的生活,我无法弄清楚我做错了什么。我知道我可能错过了一个分号或其他东西,但我真的无法弄明白。所有头文件都在下面。

DX2DHelper.h

#include "targetver.h"

#define WIN32_LEAN_AND_MEANheaders
// Windows Header Files:
#include <windows.h>

#include <iostream>

#include <D2D1.h>
#include <D2D1Helper.h>
#include <wincodec.h>
#include <D2DBaseTypes.h>
#include <D2Derr.h>

#define DLEX __declspec(dllexport)

#include "GameBase.h"
#include "SystemClass.h"
#include "DX2DImage.h"
#include "Sprite.h"

SystemClass.h

#ifndef SYSTEMCLASS_H
#define SYSTEMCLASS_H

#include "DX2DHelper.h"

namespace DX2DHelper
{
    namespace Core
    {
        class SystemClass;
        struct WindowOptions;

        struct DLEX WindowOptions {};

        class DLEX SystemClass
        {
        public:
            bool Initialize(WindowOptions options, HINSTANCE hInst);
            void Run(GameBase* game, DX2DInitOptions options);
        private:
            WindowOptions options;    
        };
    }
}
#endif

DX2DImage.h

#ifndef DX2DIMAGE_H
#define DX2DIMAGE_H

#include "DX2DHelper.h"

namespace DX2DHelper
{
    namespace Resources
    {
        class DX2DImage;
        class DX2DImageLoader;

        class DLEX DX2DImageLoader
        {
        public:
            HRESULT LoadFromFile(char* filePath, DX2DImage* image);
        };

        class DLEX DX2DImage
        {
        public:
            friend class DX2DImageLoader;
        };
    }
}

#endif

Sprite.h

#ifndef SPRITE_H
#define SPRITE_H

#include "DX2DHelper.h"

namespace DX2DHelper
{
    namespace GameComponents
    {
        class DLEX Sprite
        {
        public:
            void SetImage(DX2DHelper::Resources::DX2DImage* image);

        private:
            DX2DHelper::Resources::DX2DImage* image;
        };
    }
}
#endif

GameBase.h

#ifndef GAMEBASE_H
#define GAMEBASE_H

#include "DX2DHelper.h"

namespace DX2DHelper
{
    namespace Core
    {
        class DLEX GameBase
        {
        public:
            friend class SystemClass;

            HRESULT Initialize(HINSTANCE hInst, HWND winHandle, struct DX2DInitOptions options);
        };

        struct DLEX DX2DInitOptions {};
    }
}
#endif

如果有人能弄清楚错误,我将非常感激。我一直在查看相同的文件,现在可能接近一小时,一小半,而我无法修复它。

1 个答案:

答案 0 :(得分:3)

您的标题包含(很多)包含周期。例如,DX2DHelper.h包括Sprite.h包括DX2DHelper.h。这可能是导致错误的原因。这段代码不是Java,那些不是import语句:#include循环完全kill编译,即使你没有特别说明编译错误。选择头文件的非循环顺序。

我建议DX2DHelper.h底部没有任何包含内容。 Sprite.h可以包含DX2DImage.h

您的对象具有循环依赖关系(例如,SystemClassGameBase的情况),您将需要另一种解决方案。前向声明依赖类而不给出完整声明,例如在SystemClass.h中你可以提前添加某个地方:

namespace DX2DHelper { namespace Core { class DLEX GameBase; } }

,类似于GameBase.h

namespace DX2DHelper { namespace Core { class DLEX SystemClass;} }

当没有涉及循环对象依赖项时,这甚至可能是一个好主意 - 它通常是一种简单的方法,围绕你在这里遇到的标题类型。当然,要编译所有内容,您可能需要#include相应实现文件中的相应标头。但是,假设您没有#include任何.cpp / .cc个文件,这应该允许在没有#include地狱的情况下编译所有内容。< / p>