首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

大家帮忙看一上这份C++代码为什么通不过

2012-09-05 
大家帮忙看一下这份C++代码为什么通不过啊#includeiostream#includecmathusing namespace stdclass P

大家帮忙看一下这份C++代码为什么通不过啊
#include<iostream>
#include<cmath>
using namespace std;

class Point{
public:
  Point(int x=0,int y=0):x(x),y(y){}
  int getX(){return x;}
  int getY(){return y;}
  friend double distant(Point &p1,Point &p2);
private:
  int x,y;
};
double distant(Point &p1,Point &p2)
{
  double x=p1.x-p1.x;
  double y=p2.y-p2.y;  
  return static_cast<double>(sqrt(x*x,y*y));  
}
int main()
{
  Point myp1(1,1),myp2(4,5);  
  cout<<"The distance is: ";
  cout<<distant(myp1,myp2);
  system("pause");
  return 0;
}
我用的是DevC++编译的,编译出现错误,错误在sqrt()。错误如下:
 F:\C语言及专业课课件\cpp类\5_6111.cpp In function `double distant(Point&, Point&)': 
18 F:\C语言及专业课课件\cpp类\5_6111.cpp no matching function for call to `sqrt(double, double)' 
 note D:\Program Files\Dev-Cpp\include\math.h:151 candidates are: double sqrt(double) 
 note D:\Program Files\Dev-Cpp\include\math.h:151 long double std::sqrt(long double) 
 note D:\Program Files\Dev-Cpp\include\math.h:151 float std::sqrt(float) 
但是你如果将那个distant函数中的double全改为float的话就可以。
不明白问题出在哪里?

[解决办法]
楼主写错了吧: return static_cast<double>(sqrt(x*x,y*y)); 应该是x*x+y*y吧
sqrt函数只接受一个参数,并且计算距离公式不对。这样:

C/C++ code
#include<iostream>#include<cmath>using namespace std;class Point{public:    Point(int x=0,int y=0):x(x),y(y){}    int getX(){return x;}    int getY(){return y;}    friend double distant(Point &p1,Point &p2);private:    int x,y;};double distant(Point &p1,Point &p2){    double x=p1.x-p2.x;    double y=p1.y-p2.y;       return static_cast<float>(sqrt(x*x+y*y));     }int main(){    Point myp1(1,1),myp2(4,5);       cout<<"The distance is: ";    cout<<distant(myp1,myp2);    system("pause");    return 0;}
[解决办法]
return static_cast<double>(sqrt(x*x,y*y));
应该是加法吧,逗号表达式等于sqrt(y*y),
而且sqrt返回值就是double,不用转换
[解决办法]
探讨

楼主写错了吧: return static_cast<double>(sqrt(x*x,y*y)); 应该是x*x+y*y吧
sqrt函数只接受一个参数,并且计算距离公式不对。这样:
C/C++ code

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

class Point
{
public:
Point(in……

热点排行