关于sqrt函数 error C2668: 'sqrt' : ambiguous call to overloaded function

来源:互联网 发布:centos mv 移动文件夹 编辑:程序博客网 时间:2024/05/17 01:04
废话不多说直接上错误信息:

 

1>------ 已启动生成: 项目: Prime, 配置: Debug Win32 ------
1>正在编译...
1>TestPrime.cpp
1>d:\my documents\visual studio 2005\myprojects\prime\prime\testprime.cpp(49) : error C2668: 'sqrt' : ambiguous call to overloaded function
1>        d:\vs2005\vc\include\math.h(581): could be 'long double sqrt(long double)'
1>        d:\vs2005\vc\include\math.h(533): or 'float sqrt(float)'
1>        d:\vs2005\vc\include\math.h(128): or 'double sqrt(double)'
1>        while trying to match the argument list '(int)'
1>生成日志保存在“file://d:\My Documents\Visual Studio 2005\MyProjects\Prime\Prime\Debug\BuildLog.htm”
1>Prime - 1 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========

 

该错误显示sqrt函数后面参数只能是long double 、float或者double类型的数据,如果给它一个整形的参数,系统报错,不知道该用哪个。这时就需要我们来想办法了。

#include < iostream >
using namespace std;
#include < cmath >
#include "stdlib.h"

int main()
{
 cout << int (sqrt(float(15)));//此项为解决项
 //cout<<sqrt(15);//此项为报错项

 system("pause");
 return 0;
}

我用的是VS2005,C++编程。想让开平方后结果任然是整形需要强制转换成一个sqrt不产生歧义的方法。如上本人先把整形 15 转为 float型,然后把开方后的结果强制转化成int型。

原创粉丝点击