2012-02-23 4 views
0

순환 의존성 코드가 있습니다.순환 형 typedef 종속성을 해결하는 적절한 방법입니까?

된 a.h 파일 :

#ifndef A_H 
#define A_H 

#include "b.h" 

typedef struct { 
    b_t *test; 
} a_t; 

#endif 

된 b.h 파일 :

#ifndef B_H 
#define B_H 

#include "a.h" 

typedef struct { 
    a_t *test; 
} b_t; 

#endif 

난 그냥 내 솔루션은 그 문제를 해결하기 위해 "적절한 방법"입니다 있는지 알고 싶었다. 나는 선명하고 깨끗한 코드를 원한다.

새로운 a.h 파일 :

#ifndef A_H 
#define A_H 

#include "b.h" 

typedef struct b_t b_t; 

struct a_t { 
    b_t *test; 
}; 

#endif 

새로운 b.h 파일 : 당신의 접근 방식과

#ifndef B_H 
#define B_H 

#include "a.h" 

typedef struct a_t a_t; 

struct b_t { 
    a_t *test; 
}; 

#endif 
+0

구글 "불완전 유형". – wildplasser

+0

typedef를 사용하여 코드를 난독 화하는 대신 구조체를 사용하는 데는 하나의 인수가 있습니다. –

답변

3

문제가 a_t에 대한 typedefb.h에 있고, 그 반대의 경우도 마찬가지이다.

다소 청소기 방법은 다음과 같이 선언에서 구조체와 typedef 및 사용 구조 태그를 유지하는 것입니다 :

#ifndef A_H 
#define A_H 

struct b_t; 

typedef struct a_t { 
    struct b_t *test; 
} a_t; 

#endif 

BH

#ifndef B_H 
#define B_H 

struct a_t; 

typedef struct b_t { 
    struct a_t *test; 
} b_t; 

#endif 
관련 문제