gpt4 book ai didi

double 类型的 C 函数有时在某些头文件中声明时可以工作

转载 作者:行者123 更新时间:2023-11-30 16:25:09 26 4
gpt4 key购买 nike

我有一个奇怪的问题,有时这个函数并不总是有效,并且只有在调用它的同一 header 中声明它时才有效...这是一件非常奇怪的事情,因为编译器看起来甚至忽略包含该函数的文件是否包含...

double mapRange(double value, double r1, double r2, double n1, double n2)
{
double res = n1+(value-r1)*((n2-n1)/(r2-r1));
printf("dividingby: %f", res);putchar('\n');
return res;
}

正如您所看到的,当它打印值时,它是正确的,但是当我打印来自另一个函数(如 main)的返回值时,我得到垃圾数字......最糟糕的是我自己在一个文件上测试了它并且它工作完美,我知道这似乎是我的错误,但我真的找不到对此的解释......有人遇到过类似的事情吗?我的编译器坏了吗? C 有什么我不知道的限制吗?我也知道我没有比这更多的代码可以提供,这使得诊断变得困难,但它来自一个相当不小的大学项目......

编辑1:好的,所以

//main.c

 #ifndef UTIL_H
#define UTIL_H
#include "util.h"
#endif /*UTIL_H*/

int main(int argc, char *argv[]) {
Matrix* volcan = readVolcano("test.txt");
double t;
mapRange(7.0, 1.0, 9.0, 0.0, 1.0,&t);
printf("%f\n..\n", t);
mat_printinfo(volcan);
mat_printmat(volcan, NULL);
weighted_volcano(volcan, NULL, NULL, NULL);
mat_free(volcan);
return 0;
}

//util.c

#include "util.h"


/*
Retorna una transformacion de value que esta entre r1 y r2 a un valor que esta
entre n1 y n2, con tal que tenga la misma relacion con el rango [n1, n2] como
value tenga con el rango [r1, r2]
*/


double mapRange(double value, double r1, double r2, double n1, double n2, double* result)
{
double res = n1+(value-r1)*((n2-n1)/(r2-r1));
printf("dividingby: %f", res);putchar('\n');
if(result!=NULL)
*result = res;
return res;
}

我绝对确定定义中没有重复,我也多次重建了该项目......

D:\Code\C\volcan\cc1.exe    [Warning] command line option '-std=c++11' is valid for C++/ObjC++ but not for C
D:\Code\C\volcan\main.c In function 'main':
52 19 D:\Code\C\volcan\main.c [Warning] initialization makes pointer from integer without a cast
D:\Code\C\volcan\main.c In function 'open_volcano':
91 7 D:\Code\C\volcan\main.c [Warning] assignment makes pointer from integer without a cast
D:\Code\C\volcan\main.c In function 'test_volcano':
119 13 D:\Code\C\volcan\main.c [Warning] assignment makes pointer from integer without a cast
D:\Code\C\volcan\cc1.exe [Warning] command line option '-std=c++11' is valid for C++/ObjC++ but not for C
D:\Code\C\volcan\volcantobmp.c In function 'weighted_volcano':
38 6 D:\Code\C\volcan\volcantobmp.c [Warning] assignment makes pointer from integer without a cast
40 6 D:\Code\C\volcan\volcantobmp.c [Warning] assignment makes pointer from integer without a cast
D:\Code\C\volcan\cc1.exe [Warning] command line option '-std=c++11' is valid for C++/ObjC++ but not for C

IDE 的输出

编辑2:所以看来问题出在这里

//util.h

#include "headers.h"

里面是空的...

最佳答案

根据您的描述,您的“代码库”中的某个位置很可能存在另一个“mapRange”函数。

为了隔离问题,我建议将“mapRange”函数重命名为独特的名称,例如附加今天的日期 (mapRange_20181127),将 mapRange 调用保留在 main() 中。如果您的“代码库”包含另一个名为“mapRange”的函数,您将不会遇到任何错误,因为链接器会找到“其他”mapRange。

如果是这种情况,选择另一个有意义的名称可以解决您的问题。

希望这有帮助!

更新:我想当我打字时,您发布了更多详细信息并得到了答案:)。

关于double 类型的 C 函数有时在某些头文件中声明时可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53491499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com