循环头依赖
Circular header dependency
我被困在(我认为是)c 中 VS2017 编码中的循环依赖问题。
我尝试查找问题,并在 Whosebug 上发现了很多类似的问题,但我似乎无法通过这些问题解决我的问题。
我的代码:
main.c
#include <stdio.h>
#include "travelers.h"
#include "trip.h"
int main(void) {
int nTravelers = 0;
int nTrips = 0;
Traveler *travelersArray = (Traveler*)calloc(nTravelers, sizeof(Traveler));
Trip *tripsArray = (Trip*)calloc(nTrips, sizeof(Trip));
return 0;
}
travelers.h
typedef struct {
unsigned int id;
char *name;
char *adress;
char *residence;
} Traveler;
trip.h
typedef struct {
unsigned int id;
char *touringCar;
char *destination;
char *date;
Traveler *travelers;
unsigned int amount;
} Trip;
travelers.c
和 trip.c
文件只包含 #include "travelers.h"
/#include "trip.h"
错误仅发生在 trip.h
at Traveler *travelers;
:
我不知道如何解决这个问题。
This 看起来是同一个问题,但我无法将其转换为我的代码。
欢迎提供任何帮助。
这里没有循环。
如果 trip.c
包括 trip.h
也应包括 travelers.h
因为其定义 (Trip
) 取决于后者 (Traveller
).
知道这一点,就可以将 travelers.h
包含到 trip.h
中。尽管如此,这仍然使事情复杂化,因此最好首先添加到每个 header 中,因此调用 header-guards,以防止在 pre-processor 级别上出现重复定义。
这样做使 header 看起来像这样:
travelers.h
#ifndef TRAVELERS_H
#define TRAVELERS_H
typedef struct {
unsigned int id;
char *name;
char *adress;
char *residence;
} Traveler;
#endif // #ifndef TRAVELERS_H
trip.h
#ifndef TRIP_H
#define TRIP_H
#include "travelers.h" // makes including it unnecessary where trip.h is included
typedef struct {
unsigned int id;
char *touringCar;
char *destination;
char *date;
Traveler *travelers;
unsigned int amount;
} Trip;
#endif // #ifndef TRIP_H
提醒一下,错误是由typedef
引起的。 C 接受 opaque 结构,前提是您不需要它们的实现细节:
a.h:
struct A {
int aVal;
const char * astr;
};
a.c:
#include "a.h"
const char *getAStr(struct A*a) {
return a->astr;
}
b.h
const char *getName(struct B*);
struct B {
int bVal;
struct A *a;
};
b.c
#include "b.h"
const char *getAStr(struct A*);
const char * getName(struct B* b) {
return getAStr(b->a);
}
main.c
#include <stdio.h>
#include "a.h"
#include "b.h"
int main() {
struct A a = { 1, "foo" };
struct B b = { 2, &a };
printf("%d - %d : %s\n", b.bVal, b.a->aVal, getName(&b));
return 0;
}
编译和链接甚至没有警告,而在 b.c struct A
除了 它是一个结构之外什么都不知道。
我被困在(我认为是)c 中 VS2017 编码中的循环依赖问题。
我尝试查找问题,并在 Whosebug 上发现了很多类似的问题,但我似乎无法通过这些问题解决我的问题。
我的代码:
main.c
#include <stdio.h>
#include "travelers.h"
#include "trip.h"
int main(void) {
int nTravelers = 0;
int nTrips = 0;
Traveler *travelersArray = (Traveler*)calloc(nTravelers, sizeof(Traveler));
Trip *tripsArray = (Trip*)calloc(nTrips, sizeof(Trip));
return 0;
}
travelers.h
typedef struct {
unsigned int id;
char *name;
char *adress;
char *residence;
} Traveler;
trip.h
typedef struct {
unsigned int id;
char *touringCar;
char *destination;
char *date;
Traveler *travelers;
unsigned int amount;
} Trip;
travelers.c
和 trip.c
文件只包含 #include "travelers.h"
/#include "trip.h"
错误仅发生在 trip.h
at Traveler *travelers;
:
我不知道如何解决这个问题。
This 看起来是同一个问题,但我无法将其转换为我的代码。
欢迎提供任何帮助。
这里没有循环。
如果 trip.c
包括 trip.h
也应包括 travelers.h
因为其定义 (Trip
) 取决于后者 (Traveller
).
知道这一点,就可以将 travelers.h
包含到 trip.h
中。尽管如此,这仍然使事情复杂化,因此最好首先添加到每个 header 中,因此调用 header-guards,以防止在 pre-processor 级别上出现重复定义。
这样做使 header 看起来像这样:
travelers.h
#ifndef TRAVELERS_H
#define TRAVELERS_H
typedef struct {
unsigned int id;
char *name;
char *adress;
char *residence;
} Traveler;
#endif // #ifndef TRAVELERS_H
trip.h
#ifndef TRIP_H
#define TRIP_H
#include "travelers.h" // makes including it unnecessary where trip.h is included
typedef struct {
unsigned int id;
char *touringCar;
char *destination;
char *date;
Traveler *travelers;
unsigned int amount;
} Trip;
#endif // #ifndef TRIP_H
提醒一下,错误是由typedef
引起的。 C 接受 opaque 结构,前提是您不需要它们的实现细节:
a.h:
struct A {
int aVal;
const char * astr;
};
a.c:
#include "a.h"
const char *getAStr(struct A*a) {
return a->astr;
}
b.h
const char *getName(struct B*);
struct B {
int bVal;
struct A *a;
};
b.c
#include "b.h"
const char *getAStr(struct A*);
const char * getName(struct B* b) {
return getAStr(b->a);
}
main.c
#include <stdio.h>
#include "a.h"
#include "b.h"
int main() {
struct A a = { 1, "foo" };
struct B b = { 2, &a };
printf("%d - %d : %s\n", b.bVal, b.a->aVal, getName(&b));
return 0;
}
编译和链接甚至没有警告,而在 b.c struct A
除了 它是一个结构之外什么都不知道。