编译通不过,那个地方错了呢? const后置为什么?

harderror 2012-08-10 03:44:43

#include <iostream>
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;}
point::void setPoint(float a,float b)
{x=a;y=b;}
point::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;
}
...全文
148 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
木子小新 2012-08-10
  • 打赏
  • 举报
回复
LZ是初学者把?
fzamygsd 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

float getX() const {return x;}
>>const后置表明该函数不修改类成员
[/Quote]
+ 常对象只能直接调用长成员函数
fzamygsd 2012-08-10
  • 打赏
  • 举报
回复
把point::setPoint理解为一个完整的函数名吧

在外边不要随便分开
lylm 2012-08-10
  • 打赏
  • 举报
回复
float getX() const {return x;}
>>const后置表明该函数不修改类成员
pathuang68 2012-08-10
  • 打赏
  • 举报
回复
point::void setPoint(float a,float b)
改为
void point::setPoint(float a,float b)
shen_wei 2012-08-10
  • 打赏
  • 举报
回复
point::void setPoint(float a,float b)


这样的写法,真实第一次见!!参看1楼的写法。。
翅膀又硬了 2012-08-10
  • 打赏
  • 举报
回复
简单改了一下,只是改成能编译通过运行了,没看逻辑

#include <iostream>
using namespace std;
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;
}

64,691

社区成员

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

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