0

これは私を当惑させている非常に単純な質問です。

1 つのソース ファイルで次のエラーが発生しますが、他のソース ファイルでは発生しません。

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 が "double" を使用し、Source1 が "float" を使用しているということですが、キャストによって、'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);
4

2 に答える 2

2

ヘッダー<math.h>は、その名前をグローバル名前空間に入れます。ヘッダー<cmath>はその名前を名前空間に入れますstd。(それぞれが他の名前空間にも名前を入れることができます)。コードで を使用する場合<math.h>、sin 関数の呼び出し方は ですsin(theta)。コードが を使用する場合<cmath>、sin 関数を呼び出す方法はstd::sin(theta)(コードが abomination を使用しない限りusing namespace std) です。Source2.cpp では、#includeディレクティブが を取り込む<cmath>ため、ファイルは生の名前ではなく修飾名を使用する必要があります。または、#includeディレクティブを pull in<math.h>に変更します。これは、Source1.cpp が行うことです。

于 2013-04-25T12:47:20.457 に答える
0

-lmリンク行に for "libm" (m = math) を追加します。

于 2013-04-25T12:44:49.433 に答える