我都是用float定义的,为什么程序编译时会说从double到float截断。代码如下
#include
using namespace std;
//#define float double
class po
{
public:
po(float x=0,float y=0);
void setpo(float,float);
float getx()const{return x;}
float gety()const{return y;}
friend ostream & operator<<(ostream &,const po &);
protected:
float x,y;
};
po::po(float a, float b)
{
x=a;y=b;
}
void po::setpo(float a, float b)
{
x=a;y=b;}
ostream &operator<<(ostream &output,const po &p)
{
output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}
int main()
{
po p(3.5,4.6);
cout<<"x="<<p.getx()<<",y="<<p.gety()<<endl;
p.setpo(8.5,6.8);
cout<<"p(new):"<<p<<endl;
}
只是写个小数,编译默认把它当做 double 类型。
如: double a = 3.14
要想写成 float, 数字的后面要加一个 f , float a = 3.14f
int main()
{
po p(3.5,4.6);
// 改为 po p(3.5f,4.6f);
cout<<"x="<<p.getx()<<",y="<<p.gety()<<endl;
p.setpo(8.5,6.8);
// 改为 p.setpo(8.5f,6.8f);
cout<<"p(new):"<<p<<endl;
}
编译默认把那个小数它当做 double 类型。