c++ - 为什么我收到 clang 警告 : no previous prototype for function 'diff'

标签 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))

我在这里缺少什么??

最佳答案

既然您在 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/

相关文章:

c++ - 查看固定长度数组之间有多少字节相等的最快方法

c++ - 未显式传递参数时使用 lambda 默认为用户输入

c - 为什么会出现段错误?

c++ - 将参数传递给常量函数是否会在内存中复制?

c++ - 多维数组(或张量)和 vector 的乘积

C++ 数组值排序

c++ - 在模板函数中使用 boost::optional arguments 作为参数

python - 使用 python 的类型库指定不止一种可能的类型

java - 模式错误纠正

c++ - 将字符拆分为字符指针数组