注册 登录
编程论坛 C++教室

小女子有个class程序读不明白,好心人给讲讲!

avator123 发布于 2011-10-12 22:26, 1348 次点击
程序代码:
#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 ;
}
11 回复
#2
czsbc2011-10-12 22:36
程序代码:
#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 ;
}
#3
makebest2011-10-12 22:47
第一个问题, 那里的Point是函数的返回值,进行加法总要有结果的呀
第二个问题, 双冒号前的Point是为了说明这个Point类内部的,跟前面的相关联的.
#4
avator1232011-10-12 22:53
程序代码:
#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++,感觉太复杂了
#5
czsbc2011-10-12 22:59
Point operator+(const Point &e,const Point &d  ) ;
这里有一个默认的左操作数,就是调用operator+的对象本身,只要有一个显式形参即可。
所以会出现错误error C2804: binary 'operator +' has too many parameters
#6
avator1232011-10-12 23:09
程序代码:
#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 编辑 ]
#7
avator1232011-10-12 23:11
回复 6楼 avator123
请 czsbc 讲讲他们的区别


#8
czsbc2011-10-12 23:20
对于operator+
非成员函数就要用两个,第一个是左操作数,第二个是右操作数。
成员函数有默认的左操作数,只要一个右操作数就OK了。
书上说的很清楚
另外operator<< 必须定义为非成员函数。
#9
avator1232011-10-12 23:25
学长,果然厉害啊!晚安!
#10
czsbc2011-10-12 23:26
我也是初学。
#11
Noll_Nie2011-10-12 23:48
mark
#12
ll5649927642011-10-13 16:09
学习~
1