2014-01-29 24 views
-3

我已经在这里好几天了,无法成功编译。我做了一个非常简单的项目,与我的主要项目分开。根据其内嵌结构我注释掉,我收到以下错误之一:如何解决C中未定义的结构?

文件:x2d_gfx_speech_balloon.h 错误C2079:“sprBalloon”使用了未定义的结构“X2D_SPRITE” 错误C2079:“字体”使用了未定义的结构“X2D_FONT '

备注:将公共标题中的精灵和字体标题放入发言气球标题本身也不起作用。

我将在下面提供整个简化代码。如果你看到任何语法错误,请告诉我,如果有帮助,可以随意编译它。我的环境是使用Visual Studio 2010并使用C89的Windows 7。

的main.c

#include "common.h" 

int main(void) 
{ 
    return 0; 
} 

COMMON.H

#ifndef _COMMON_H_ 
#define _COMMON_H_ 

#include <stdio.h> 
#include <stdlib.h> 
#include "constants.h" 

#include "X2D_GFX_Sprite.h" 
#include "X2D_GFX_Font.h" 
#include "X2D_GFX_Speech_Balloon.h" 

#endif 

constants.h

#ifndef _CONSTANTS_H_ 
#define _CONSTANTS_H_ 

/* General Constants */ 
#define TRUE    1 
#define FALSE    0 

#endif 

X2D_GFX_Font.h

#ifndef _X2D_GFX_FONT_H_ 
#define _X2D_GFX_FONT_H_ 

#include "common.h" 

/* Font Structure */ 
struct X2D_FONT 
{ 
    float x, y; 
    int size; 
    int blnShow; 
}; 

/* Font Prototypes (not shown here)... */ 
int fnt_init(struct X2D_FONT *objFont, const char *strFileName, 
    const char *strText, const float x, const float y, 
    const unsigned int size); 

#endif 

X2D_GFX_Font.c

#include "X2D_GFX_Font.h" 

int fnt_init(struct X2D_FONT *objFont, const char *strFileName, 
    const char *strText, const float x, const float y, 
    const unsigned int size) 
{ 

    return TRUE; 
} 

X2D_GFX_Sprite.h

#ifndef _X2D_GFX_SPRITE_H_ 
#define _X2D_GFX_SPRITE_H_ 

#define MAX_VARSI   10 

#include "common.h" 

struct X2D_SPRITE 
{ 
    float x; 
    float y; 
    unsigned int width; 
    unsigned int height; 
    int blnShow; 
    float vx, vy; 
    float angle; 
    int varsi[MAX_VARSI]; 
}; 

int spr_init(struct X2D_SPRITE *spr, const char *strFileName); 

#endif 

X2D_GFX_Sprite.c

#include "X2D_GFX_Sprite.h" 

int spr_init(struct X2D_SPRITE *spr, const char *strFileName) 
{ 
    return TRUE; 
} 

X2D_GFX_SPEECH_BALLOON.h

#ifndef _X2D_GFX_SPEECH_BALLOON_H_ 
#define _X2D_GFX_SPEECH_BALLOON_H_ 

#include "common.h" 

/* Contains a list of acceptable balloon types */ 
enum ESpeechBalloonType 
{ 
    ESpeechBalloonType_Talk, 
    ESpeechBalloonType_Thought, 
    ESpeechBalloonType_Yell, 
    ESpeechBalloonType_Whisper 
}; 

/* Speech Balloon types */ 
struct X2D_SPEECH_BALLOON 
{ 
    struct X2D_SPRITE sprBalloon; 
    struct X2D_FONT font;  
    enum ESpeechBalloonType eBalloonType; 
}; 

#endif 

X2D_GFX_SPEECH_BUBBLE.c

#include "X2D_GFX_SPEECH_BALLOON.h" 

回答

2

您的问题是包含文件的顺序,具体是X2D_GFX_Sprite.c。如果只运行该文件的预处理器(在Linux中,您可以使用cppgcc -E,我不知道在Windows中如何)并向下滚动到最后,您会发现X2D_SPRITE结构在使用后会被声明:修复它

struct X2D_SPEECH_BALLOON 
{ 
    struct X2D_SPRITE sprBalloon; 
    struct X2D_FONT font; 
    enum ESpeechBalloonType eBalloonType; 
}; 
# 11 "common.h" 2 
# 7 "X2D_GFX_Sprite.h" 2 

struct X2D_SPRITE 
{ 
    float x; 
    float y; 
    unsigned int width; 
    unsigned int height; 
    int blnShow; 
    float vx, vy; 
    float angle; 
    int varsi[10]; 
}; 

两个最简单的方法是要么不使用common.hX2D_GFX_Sprite.h.

这是因为X2D_GFX_Sprite.c包括X2D_GFX_Sprite.h,其中包括common.h,其中包括其他结构声明后移动#include "common.h"。 h文件。预处理器首先复制X2D_GFX_Sprite.h。当它找到#include "common.h"时,它开始复制该文件。前三个包括被复制进来。当它回到X2D_GFX_Sprite.h时,它会复制它,但标头守卫将摆脱其中的所有内容。接下来,它依次拷贝X2D_GFX_Font.hX2D_GFX_Speech_Balloon.h,其中包括X2D_SPEECH_BALLOON结构。只有这样才能最终加入X2D_GFX_Sprite.h的其余部分,包括X2D_SPRITE结构。这导致这两个结构不会以正确的顺序被复制。

+0

解决了!谢谢。这是一种解脱。 – Phil

相关问题