这个小程序是怎么会事?

熊猫过隙 2008-10-13 02:11:44
#include <iostream.h> 


class 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(float a,float b)
{
x = a;
y = b;
}

void Point::setPoint(float, float)
{
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);
cout << "x=" << p.getX() << ",y=" << p.getY() << endl;
p.setPoint(8.5,6.8);
cout << "p(new):" << p << endl;


char response;
cin >> response;

return 0;
}


在vc++6.0中的编译错误提示如下:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
c:\code\cpp1.cpp(26) : error C2065: 'a' : undeclared identifier
c:\code\cpp1.cpp(26) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
c:\code\cpp1.cpp(27) : error C2065: 'b' : undeclared identifier
c:\code\cpp1.cpp(27) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
c:\code\cpp1.cpp(40) : warning C4305: 'argument' : truncation from 'const double' to 'float'
c:\code\cpp1.cpp(42) : warning C4305: 'argument' : truncation from 'const double' to 'float'
Error executing cl.exe.

Cpp1.exe - 2 error(s), 4 warning(s)


这个程序怎么调试。谢谢
...全文
85 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sealrose 2008-10-13
  • 打赏
  • 举报
回复
void Point::setPoint(float a, float b)
{
x = a;
y = b;
}



int main()
{
Point p(3.5,6.4); //改为:Point p(3.5f,6.4f); 试下
cout << "x=" << p.getX() << ",y=" << p.getY() << endl;
p.setPoint(8.5,6.8);
cout << "p(new):" << p << endl;


char response;
cin >> response;

return 0;
}
woaiyu_liang 2008-10-13
  • 打赏
  • 举报
回复
出错处见下面程序中的注释处:
#include <iostream.h>


class 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(float a,float b)
{
x = a;
y = b;
}

void Point::setPoint(float, float)//函数定义时需要给出形参名,改为void Point::setPoint(float a, float b)[/color]
{
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);//3.5与6.4皆为double型,而p函数的形参为float型,因此会出现类型不匹配的警告
cout << "x=" << p.getX() << ",y=" << p.getY() << endl;
p.setPoint(8.5,6.8);
cout << "p(new):" << p << endl;


char response;
cin >> response;

return 0;
}
wangdeqie 2008-10-13
  • 打赏
  • 举报
回复

void Point::setPoint(float a, float b)
{
x = a;
y = b;
}
coverallwangp 2008-10-13
  • 打赏
  • 举报
回复

#include <iostream.h>


class 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(float a,float b)
{
x = a;
y = b;
}

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);
cout << "x=" << p.getX() << ",y=" << p.getY() << endl;
p.setPoint(8.5,6.8);
cout << "p(new):" << p << endl;


char response;
cin >> response;

return 0;
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧