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

关于cmath中的sqrt

deng0981 发布于 2011-11-05 21:17, 1034 次点击
#include<iostream>
#include<cmath>
using namespace std;
class point
{
private:
    int x; int y;
public:
    float distance()
    {
        return sqrt(x*x+y*y);
    }
    void intit(int a, int b)
    {
    x=a; y=b;
    }
}A;
void main()
{
point B;
A.intit(3,4);
B.intit(6,8);
cout<<A.distance()<<" "<<B.distance()<<endl;
}
代码如上。用VS运行的时候有问题。用VC6.0返回值也不正确。请高手知道问题何在
2 回复
#2
Toomj2011-11-06 00:34
在VC6.0中的math.h头文件的函数原型为double sqrt(double); VS 2008后为重载函数,原型为 float sqrt (float),double sqrt (double),double long sqrt(double long)   注意没有int sqrt (int)
#3
deng09812011-11-06 13:36
回复 2楼 Toomj
问题解决了,6.0是可以用int, vs要用double.
1