我使用矢量操作和引力常数创建重力模拟器。 我为尸体定义了2个结构。
typedef struct {
double vector[3];
} Vector;
typedef struct {
Vector colour;
double mass;
double radius;
Vector position;
Vector velocity;
Vector accel;
} Object;
我有很多矢量运算函数,包括:
Vector VectorUnit(Vector a) {
Vector b;
int i;
for (i = 0; i < VECTOR_DIM; i++)
b.vector[i] = (a.vector[i]) / (VectorMag(a));
return (b);
}
当我运行函数的内部时,它编译得很好。虽然当我使用任何向量数量显式调用函数VectorUnit()时它声称“冲突类型”错误..
GRAV.c:463:8: error: conflicting types for ‘VectorUnit’
Vector VectorUnit(Vector a)
^
GRAV.c:341:3: note: previous implicit declaration of ‘VectorUnit’ was here
VectorUnit(bodies[j].position);
它与函数调用有什么问题,例如VectorUnit(bodies [j] .position); 如上所述,使用我的函数的内部编译完美无缺..
答案 0 :(得分:0)
您正在调用函数而不先声明它们。这是自C99以来的非法行为。在C89中,它意味着隐式地将函数声明为返回int
。
您需要在调用函数之前为其提供原型,例如:有一个头文件:
Vector VectorUnit(Vector a);
或者如果该函数仅出现在一个.c
文件中,请在文件顶部附近声明:
static Vector VectorUnit(Vector a);
或以不同的顺序放置您的功能,以便VectorUnit
的正文出现在任何调用它的函数之前。