我的代码中有以下声明:
//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))
我在这里缺少什么??
最佳答案
既然您在 header 中定义(而不是声明)您的函数,那么您应该将其内联。变化:
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 文件中,并在头文件中保留原型(prototype)。
关于c++ - 为什么我收到 clang 警告 : no previous prototype for function 'diff' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21368822/