我的代码中包含以下声明:

//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++ - 为什么我收到c警告: no previous prototype for function 'diff' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21368822/

10-09 05:58
查看更多