c++ - 找不到 sin(double)、sin(double&)、cos(double)、cos(double&)

标签 c++ linker trigonometry

这是一个很简单的问题,让我很困惑。

我收到一个源文件的以下错误,但另一个没有:

4  src/Source2.cpp:1466: error: no matching function for call to ‘cos(double&)’
5  src/Source2.cpp:1466: error: no matching function for call to ‘sin(double)’
6  src/Source2.cpp:1467: error: no matching function for call to ‘sin(double&)’
7  src/Source2.cpp:1467: error: no matching function for call to ‘sin(double)’
8  src/Source2.cpp:1468: error: no matching function for call to ‘cos(double)’
9  src/Source2.cpp:1479: error: no matching function for call to ‘cos(double&)’
10 src/Source2.cpp:1479: error: no matching function for call to ‘sin(double)’
11 src/Source2.cpp:1480: error: no matching function for call to ‘sin(double&)’
12 src/Source2.cpp:1480: error: no matching function for call to ‘sin(double)’
13 src/Source2.cpp:1481: error: no matching function for call to ‘cos(double)’

这很奇怪,因为我有 Header1.hpp/Source1.cpp 工作,但 Header2.hpp/Source2.cpp 不工作。它们之间的区别在于 Source2 使用的是“doubles”而 Source1 使用的是“floats”,但是使用“sin(float)”或“cos(float)”进行转换时会出现与上面相同的错误。

我想我正在链接数学库,因为其他来源(相同的程序)可以工作并且不会提示。

感谢任何建议 =)

提前致谢!

代码片段:

Header1.hpp:

4  #include <iostream>
5
6  #include <stdio.h>
7  #include <math.h>
8  #include <ctime>
9
10 #define GL_GLEXT_PROTOTYPES
11
13 #include <OpenGL/gl.h>
14 #include <GLUT/glut.h>

源1.cpp:

123   aim[0] = cos(camTheta)*sin(camPhi);
124   aim[1] = sin(camTheta)*sin(camPhi);
125   aim[2] = cos(camPhi);
126 
127   up[0] = cos(camTheta)*sin(camPhi - pih);
128   up[1] = sin(camTheta)*sin(camPhi - pih);
129   up[2] = cos(camPhi - pih);

Header2.hpp:

4  #include <algorithm>
5  #include <iostream>
6  #include <fstream>
7  #include <sstream>
8  #include <vector>
9  #include <cmath>
. . .
25 #define pih (M_PI/2.0)
26 #define pi M_PI
27 #define pi2 (2.0*M_PI)

Source2.cpp:

1453   double theta, phi;
1454   double x, y, z;
. . .
1464     node(n,0) = cos(theta)*sin(phi - pih);
1465     node(n,1) = sin(theta)*sin(phi - pih);
1466     node(n,2) = cos(phi - pih);

最佳答案

header <math.h>将其名称放入全局命名空间。标题 <cmath>将其名称放入命名空间 std . (也允许每个人将名称放入另一个 namespace )。当代码使用 <math.h> 时,调用sin函数的方式是sin(theta) .当代码使用 <cmath> 时,调用sin函数的方式是std::sin(theta) (除非代码使用可憎的 using namespace std )。在 Source2.cpp 中,#include指令拉入 <cmath> , 因此文件应使用限定名称而不是原始名称。或者更改 #include指令拉入 <math.h> ,这就是 Source1.cpp 所做的。

关于c++ - 找不到 sin(double)、sin(double&)、cos(double)、cos(double&),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16215099/

相关文章:

c++ - TCP/IP - 用 C/C++ 发送 HEX 数据包来控制继电器板

c++ - 保存应用程序设置 MFC CArchive 方式

c++ - printf 如何知道格式参数的长度?

c++ - gcc 在编译时或链接时的可见性

c++ - 数组调试不正确的输出,复杂的算法

c - 链接到静态库的多个版本

c - 如何获取 ffmpeg 链接到的库列表(静态 ffmpeg 库)?

c - 如何从固定大小的查找表中查找不同频率的正弦波?

c - C 中 arcsin 的近似

java - 尝试围绕一个点旋转,但无论如何都会失去精度