【问题标题】:Cannot find sin(double), sin(double&), cos(double), cos(double&)找不到 sin(double)、sin(double&)、cos(double)、cos(double&)
【发布时间】:2013-04-25 12:42:54
【问题描述】:

这是一个令我困惑的非常简单的问题。

一个源文件出现以下错误,另一个源文件没有:

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>

Source1.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);

【问题讨论】:

  • Source2.cpp 是否包含 Header1.hpp 或 Header2.hpp(或其他任何内容)?
  • 是的,每个都在顶部包含相应的标题。 Source2 只包含 Header2,与 Source1 类似
  • 你应该尽量保持一致。 math.hcmath。如果你满足于cmath,那么在这些函数前面加上std::
  • Source2 是否有using namespace std?标准没有指定这些函数(即在cmath 中)是否也在全局命名空间中或仅在std 中。
  • 是的,@pete-becker 回答正确。谢谢大家!

标签: c++ linker trigonometry


【解决方案1】:

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

【讨论】:

  • 哇,解决了!我不知道,谢谢=) 9分钟不让我勾选,我会在时间限制后做
  • @Yuuta - 这是一般模式:C 标头以 .h 结尾,相应的 C++ 标头以 c 开头并且没有扩展名。 C 标头将名称放入全局命名空间(可能会放入 std),C++ 标头将名称放入 std(可能放入全局命名空间)。
  • 太棒了,感谢您的额外解释。我还有很多东西要学哈哈
【解决方案2】:

将“libm”(m = 数学)的 -lm 添加到您的链接行。

【讨论】:

  • 感谢您的快速回复,我已经在链接数学库了。