首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-08-29 
编译通不过,那个地方错了呢? const后置为什么?C/C++ code#include iostreamclass point{public:point(fl

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

C/C++ code
#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;} 


[解决办法]
简单改了一下,只是改成能编译通过运行了,没看逻辑
C/C++ code
#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;}
[解决办法]
float getX() const {return x;}
>>const后置表明该函数不修改类成员

热点排行