为什么我会收到clang警告:没有以前的函数'diff'的原型

时间:2014-01-26 20:32:53

标签: c++ function pointers warnings clang

我的代码中有以下声明:

//Central diff function, makes two function calls, O(h^2)
REAL diff(const REAL h, const REAL x, REAL (*func)(const REAL))
{
    // diff = f(x + h) - f(x -h)/2h + O(h^2)
    return ((*func)(x + h) - (*func)(x - h))/(2.0*h + REALSMALL);
}

这是一个“utils.h”文件。当我使用它编译测试时,它给了我:

clang++ -Weverything tests/utils.cpp -o tests/utils.o   
In file included from tests/utils.cpp:4:
tests/../utils/utils.h:31:6: warning: no previous prototype for function 'diff' [-Wmissing-prototypes]
REAL diff(const REAL h, const REAL x, REAL (*func)(const REAL))

我在这里缺少什么?

1 个答案:

答案 0 :(得分:5)

由于在标题中定义了(不是声明)您的函数,因此您应该将其设置为内联。变化:

REAL diff(const REAL h, const REAL x, REAL (*func)(const REAL))

为:

inline REAL diff(const REAL h, const REAL x, REAL (*func)(const REAL))

或者只是将定义移动到.c文件中,并在头文件中保留原型。