c++按照书上抄下来的程序没法运行,显示不能将参数 2 从“double (__cdecl *)(double)”转换为“float”

#include<iostream>
using namespace std;
class myPoint
{
public:
myPoint(){}
myPoint(float a,float b){x=a;y=b;}
float getX(){return x;}
float getY(){return y;}
void setX(float X){x=X;}
void setY(float Y){y=Y;}
private:
float x;
float y;
};
class myLine
{
public:
myLine(float, float, float, float);
~myLine(){}
float distance();
void setXY(float, float, float, float);
private:
myPoint p1,p2;
};
myLine::myLine(float x1,float yl,float x2,float y2):p1(x1,y1),p2(x2,y2)
{
cout<<"myLine构造函数初始化表:p1("<<p1.getX()<<","<<p1.getY()<<");p2("<<p2.getX()<<","<<p2.getY()<<")"<<endl;
}
float myLine::distance()
{
cout<<"两端点("<<p1.getX()<<","<<p1.getY()<<"),("<<p2.getX()<<","<<p2.getY()<<")距离为:";
return sqrt(pow(p2.getX()-p1.getX(),2)+pow(p2.getY()-p1.getY(),2));
}
void myLine::setXY(float xx1,float yy1,float xx2,float yy2)
{
p1.setX(xx1);p1.setY(yy1);
p2.setX(xx2);p2.setY(yy2);
}
void main(void)
{
float x1,y1,x2,y2;
cout<<"请输入点1(x1,y1)和点2( x2,y2)坐标:"<<endl;
cin>>x1>>y1>>x2>>y2;
myLine testL(x1,y1,x2,y2);
cout<<testL.distance()<<endl;
testL.setXY(0,0,34,45);
cout<<testL.distance()<<endl;
}

有一个地方抄错了,第27行

`myLine::myLine(float x1,float yl,float x2,float y2):p1(x1,y1),p2(x2,y2)`

yl(小写字母l)应为y1(数字1),改了这个地方可以编译通过

#include <iostream> 
#include <cmath>
using namespace std;
class myPoint
{
public:
myPoint(){} 
myPoint(float a,float b){x=a;y=b;}
float getX(){return x;} 
float getY(){return y;} 
void setX(float X){x=X;} 
void setY(float Y){y=Y;}
private:
float x; 
float y;
};
class myLine
{
public:
myLine(float, float, float, float);
~myLine(){}
float distance(); 
void setXY(float, float, float, float);
private:
myPoint p1,p2;
};
myLine::myLine(float x1,float yl,float x2,float y2):p1(x1,y1),p2(x2,y2) // yl 应为 y1
{
cout<<"myLine构造函数初始化表:p1("<<p1.getX()<<","<<p1.getY()<<");p2("<<p2.getX()<<","<<p2.getY()<<")"<<endl;
}
float myLine::distance()
{
cout<<"两端点("<<p1.getX()<<","<<p1.getY()<<"),("<<p2.getX()<<","<<p2.getY()<<")距离为:";
return sqrt(pow(p2.getX()-p1.getX(),2)+pow(p2.getY()-p1.getY(),2));
}
void myLine::setXY(float xx1,float yy1,float xx2,float yy2)
{
p1.setX(xx1);p1.setY(yy1);
p2.setX(xx2);p2.setY(yy2);
}
int main(void)
{
float x1,y1,x2,y2;
cout<<"请输入点1(x1,y1)和点2( x2,y2)坐标:"<<endl;
cin>>x1>>y1>>x2>>y2;
myLine testL(x1,y1,x2,y2);
cout<<testL.distance()<<endl;
testL.setXY(0,0,34,45);
cout<<testL.distance()<<endl;

return 0;
}

温馨提示:答案为网友推荐,仅供参考