我有两个文件(average.h,average.c)。在我使用的结构的定义中#ifdef INTERNAL
。
#ifndef AVERAGE_H_
#define AVERAGE_H_
typedef struct str_internal {
int i1;
int i2;
} internal;
typedef struct str_speed {
int lowest;
int highest;
#ifdef INTERNAL
internal i;
#endif
} speed;
extern double average(speed twoStroke);
#endif /* AVERAGE_H_ */
average.c 的内容是,
#include <stdio.h>
#include "average.h"
double average(speed twoStroke) {
double average = (twoStroke.highest + twoStroke.lowest) / 2.0;
#ifdef INTERNAL
average += twoStroke.internal.i1+twoStroke.internal.i2;
#endif
return average;
}
int main() {
speed twoStrokeEngine;
twoStrokeEngine.highest = 70;
twoStrokeEngine.lowest = 50;
#ifdef INTERNAL
twoStrokeEngine.internal.i1=20;
twoStrokeEngine.internal.i2=10;
#endif
double avg = average(twoStrokeEngine);
printf("Average speed of two stroke engine is: %f", avg);
return 0;
}
当我尝试编译此代码时, gcc -DINTERNAL -o average.exe average.c 我收到以下错误消息。
average.c:8:22: error: ‘speed {aka struct str_speed}’ has no member named ‘internal’
average += twoStroke.internal.i1+twoStroke.internal.i2;
^
average.c:8:44: error: ‘speed {aka struct str_speed}’ has no member named ‘internal’
average += twoStroke.internal.i1+twoStroke.internal.i2;
^
average.c: In function ‘main’:
average.c:20:17: error: ‘speed {aka struct str_speed}’ has no member named ‘internal’
twoStrokeEngine.internal.i1=20;
^
average.c:21:17: error: ‘speed {aka struct str_speed}’ has no member named ‘internal’
twoStrokeEngine.internal.i2=10;