注册 登录
编程论坛 C++教室

cmath中的pow函数

kisscjy 发布于 2007-10-25 01:03, 7536 次点击
当我使用G++编译器的时候
用到了cmath中的pow函数~~

x=pow(2,3);

结果就出了一大堆错误,郁闷啊

把错误信息给大家看一下,希望大家给个意见~

79006\Main.cc:14: error: call of overloaded `pow(int, int&)' is ambiguous
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/math.h:150: note: candidates are: double pow(double, double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:345: note: float std::pow(float, float)
79006\Main.cc:17: error: call of overloaded `pow(int, int&)' is ambiguous
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/math.h:150: note: candidates are: double pow(double, double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:345: note: float std::pow(float, float)
79006\Main.cc:24: error: call of overloaded `pow(int, int&)' is ambiguous
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/math.h:150: note: candidates are: double pow(double, double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:345: note: float std::pow(float, float)

但是在VC6.0中就完全正确~~
不解啊~~~
10 回复
#2
永夜的极光2007-10-25 08:16
据说pow()换成pow<double>()就行

G++对类型的要求比较严格,你的调用产生了二义性,可以使用强制转换


int a,b;
pow(a,b) 可改成 pow(long double(a),b)

[此贴子已经被作者于2007-10-25 8:20:15编辑过]

#3
nuciewth2007-10-25 08:18
x=pow(2.0,3.0);

应该是这个编译器的类型检查更严格.
#4
kisscjy2007-10-25 08:19

好像不行~~改了之后依然会有错无~~

79034\Main.cc: In function `int main()':
79034\Main.cc:17: error: no matching function for call to `pow(int, int&)'
79034\Main.cc:20: error: no matching function for call to `pow(int, int&)'
79034\Main.cc:27: error: call of overloaded `pow(int, int&)' is ambiguous
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/math.h:150: note: candidates are: double pow(double, double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:345: note: float std::pow(float, float)

#5
永夜的极光2007-10-25 08:21

G++对类型的要求比较严格,你的调用产生了二义性,可以使用强制转换


int a,b;
pow(a,b) 可改成 pow(long double(a),b)

#6
kisscjy2007-10-25 08:24

我现在的问题是这样~

int x=2, y=3;

pow( x,y )

这样的话该怎样改呢??

#7
nuciewth2007-10-25 08:32

强制类型转换

#8
永夜的极光2007-10-25 08:39
pow(long double(x),y)
#9
herolzx2007-10-25 08:42

我也遇到过,干脆自己写一个int pow(int,int)函数算了,也不难。
int pow(int a,int b){
int s=1;
for(int i=0,i<b,i++)s= s* a;
return s;
}

#10
kisscjy2007-10-25 08:43

我晕了~~还不行.....................
我把我的代码帖上来吧~~~
[CODE]
/*---------------------------------------------------------------


Description

  集合的前N个元素:编一个程序,按递增次序生成集合M的最小的N个数,M的定义如下:
(1)数1属于M;
(2)如果X属于M,则Y=2*X+1和Z=3*x+1也属于M;
(3)此外再没有别的数属于M。


Input

n(b.in) (1≤n≤100)

Output

生成集合M的最小的N个数(b.out)

Sample Input


4

Sample Output


1 3 4 7

-----------------------------------------------------------------*/


#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;


int main()
{
int num, i, j, k;
cin>>num;

int sum=0;
for( i=0; num > sum; i++ )
sum = sum + pow(2.0,double(i));

k=i-1;
int *p=new int [pow(2.0,double(k))];
vector< int > vec;

p[0]=1, vec.push_back(1);

for( i=1; i<=k; i++ )
{
for( j=pow(2,i)-1; j>=0; j--)
{
if( j % 2 !=0 )
p[j] = 3 * p[j/2] + 1;

else
p[j] = 2 * p[j/2] + 1;

vec.push_back( p[j] );
}
}

sort( vec.begin(), vec.end() );

for( i=0; i<num; i++ )
cout<<vec[i]<<" ";

cout<<endl;

return 0;
}



[/CODE]

#11
kisscjy2007-10-25 08:44

顺便也把错误帖上来好了~~


[CODE]


79039\Main.cc: In function `int main()':
79039\Main.cc:21: error: expression in new-declarator must have integral or enumeration type
79039\Main.cc:28: error: call of overloaded `pow(int, int&)' is ambiguous
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/math.h:150: note: candidates are: double pow(double, double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:361: note: long double std::pow(long double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:357: note: float std::pow(float, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:353: note: double std::pow(double, int)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:349: note: long double std::pow(long double, long double)
bin/gcc/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/cmath:345: note: float std::pow(float, float)




[/CODE]

1