为什么会报如下的类型转换警告?
编译下面一段代码:
#include<iostream>using namespace std;//声明类Pointclass Point{public:Point(float x=0,float y=0);void setPoint(float,float);float getX() const{return x;}float getY() const{return y;}friend ostream & operator<<(ostream &,const Point &);protected: float x,y;};//下面定义Point类的成员函数//Point的构造函数Point::Point(float a,float b){x=a;y=b;}//设置x和y的坐标值void Point::setPoint(float a,float b){x=a;y=b;}//重载运算符“<<”,使之能输出点的坐标ostream & operator<<(ostream &output,const Point &p){output<<"["<<p.x<<","<<p.y<<"]"<<endl;return output;}int main(){Point p(3.5,6.4);//[color=#FF0000]warning C4305: “参数”: 从“double”到“float”截断[/color]cout<<"x="<<p.getX()<<","<<"y="<<p.getY()<<endl;p.setPoint(8.5,6.8);//[color=#FF0000]warning C4305: “参数”: 从“double”到“float”截断[/color]cout<<"p(new):"<<p<<endl;}Point p(3.5f,6.4f); //加上f p.setPoint(8.5f,6.8f); //加上f return 0; //加上返回值
[解决办法]