| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1348 人关注过本帖
标题:小女子有个class程序读不明白,好心人给讲讲!
只看楼主 加入收藏
avator123
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-12-3
结帖率:80%
收藏
已结贴  问题点数:0 回复次数:11 
小女子有个class程序读不明白,好心人给讲讲!
程序代码:
#include <iostream>
using namespace std;

class Point;
ostream& operator<<(ostream& o, const Point& d);

class Point {

    int x , y ;

    public :

    void set (int a ,int b) {   x = a ,y = b ; }

    Point operator+(const Point& d ) ;  //here ,为什么要用Point operator+(const Point&d)而不是operator+(const Point& d) ?
 


    friend ostream& operator<<(ostream& o, const Point& d) ;


} ;

Point Point::operator+(const Point &d ) //这两个Point 各自什么意思?可以给讲讲吗?为什么用两个Point ?
{

 

    Point s ;
    s.set (x+d.x,y+d.y);
    return s ;

 

    }

ostream& operator<<(ostream& o, const Point& d)  {


return o<<"("<<d.x<<", "<<d.y<<")\n"  ;

}

void main()
{
Point s,t ;
s.set(2,5);
t.set(3,1);
cout<<s+t ;
}
搜索更多相关主题的帖子: 小女子 class 
2011-10-12 22:26
czsbc
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:469
专家分:1700
注 册:2008-12-13
收藏
得分:3 
程序代码:
#include <iostream>
using namespace std;

class Point;
ostream& operator<<(ostream& o, const Point& d);

class Point {

    int x , y ;

    public :

    void set (int a ,int b) {   x = a ,y = b ; }

    Point operator+(const Point& d ) ;  //here ,Point 是返回类型

    friend ostream& operator<<(ostream& o, const Point& d) ;


} ;

Point Point::operator+(const Point &d ) //  第一个是返回类型,第二个是表示是Point 的成员函数,放在作用域操作符::前面。
{


    Point s ;
    s.set (x+d.x,y+d.y);
    return s ;


    }

ostream& operator<<(ostream& o, const Point& d)  {


return o<<"("<<d.x<<", "<<d.y<<")\n"  ;

}

void main()
{
Point s,t ;
s.set(2,5);
t.set(3,1);
cout<<s+t ;
}
2011-10-12 22:36
makebest
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:658
专家分:962
注 册:2005-3-17
收藏
得分:3 
第一个问题, 那里的Point是函数的返回值,进行加法总要有结果的呀
第二个问题, 双冒号前的Point是为了说明这个Point类内部的,跟前面的相关联的.
2011-10-12 22:47
avator123
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-12-3
收藏
得分:0 
程序代码:
#include <iostream>

using namespace std;

class Point;

ostream& operator<<(ostream& o, const Point& d);

class Point {

    int x , y ;

    public :
    void set (int a ,int b) {   x = a ,y = b ;}

   

    Point operator+(const Point &e,const Point &d  ) ;

 

    friend ostream& operator<<(ostream& o, const Point& d) ;
} ;

Point Point::operator+(const Point &e,const Point &d )
{

 

    Point s ;
    s.set (e.x+d.x,e.y+d.y);
    return s ;

 

    }

ostream& operator<<(ostream& o, const Point& d)  {


return o<<"("<<d.x<<", "<<d.y<<")\n"  ;

}

void main()
{
Point s,t ;
s.set(2,5);
t.set(3,1);
cout<<s+t;

}
--------------------Configuration: training - Win32 Debug--------------------
Compiling...
training.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\myproject\training.cpp(17) : error C2804: binary 'operator +' has too many parameters
D:\Program Files\Microsoft Visual Studio\MyProjects\myproject\training.cpp(43) : error C2676: binary '+' : 'class Point' does not define this operator or a conversion to a type acceptable to the predefined operator
执行 cl.exe 时出错.

training.obj - 1 error(s), 0 warning(s)

帮我看看,错在哪里?我刚学C++,感觉太复杂了
2011-10-12 22:53
czsbc
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:469
专家分:1700
注 册:2008-12-13
收藏
得分:0 
Point operator+(const Point &e,const Point &d  ) ;
这里有一个默认的左操作数,就是调用operator+的对象本身,只要有一个显式形参即可。
所以会出现错误error C2804: binary 'operator +' has too many parameters
2011-10-12 22:59
avator123
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-12-3
收藏
得分:0 
程序代码:
#include <iostream>

using namespace std;

class Point;

Point operator+(const Point &e,const Point &d );
ostream& operator<<(ostream& o, const Point& d);

class Point {

    int x , y ;

    public :
    void set (int a ,int b) {   x = a ,y = b ;}

    friend Point operator+(const Point &e,const Point &d  ) ;

 

    friend ostream& operator<<(ostream& o, const Point& d) ;
} ;

Point operator+(const Point &e,const Point &d )   /* 为什么 这里要定义两个参数const Point &e,const Point &d,而上边的程序只需要一个参数既可以?Point operator+                (const Point &e,const Point &d  ) ;
这里有一个默认的左操作数,就是调用operator+的对象本身,只要有一个显式形参即可。
所以会出现错误error C2804: binary 'operator +' has too many parameters ,他们区别在哪里?

*/
{

 

    Point s ;
    s.set (e.x+d.x,e.y+d.y);
    return s ;

 

    }

ostream& operator<<(ostream& o, const Point& d)  {


return o<<"("<<d.x<<", "<<d.y<<")\n"  ;

}

void main()
{
Point s,t ;
s.set(2,5);
t.set(3,1);
//operator+(s,t);
cout<<s+t ;

}






[ 本帖最后由 avator123 于 2011-10-12 23:12 编辑 ]
2011-10-12 23:09
avator123
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-12-3
收藏
得分:0 
回复 6楼 avator123
请 czsbc 讲讲他们的区别


2011-10-12 23:11
czsbc
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:469
专家分:1700
注 册:2008-12-13
收藏
得分:0 
对于operator+
非成员函数就要用两个,第一个是左操作数,第二个是右操作数。
成员函数有默认的左操作数,只要一个右操作数就OK了。
书上说的很清楚
另外operator<< 必须定义为非成员函数。
2011-10-12 23:20
avator123
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-12-3
收藏
得分:0 
学长,果然厉害啊!晚安!
2011-10-12 23:25
czsbc
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:469
专家分:1700
注 册:2008-12-13
收藏
得分:0 
我也是初学。
2011-10-12 23:26
快速回复:小女子有个class程序读不明白,好心人给讲讲!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027145 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved