驾驶证可以借人扣分吗:一个懂CC++的人,不应该不会的问题?

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/28 16:08:13
这是程序代码:
#include <iostream>
using namespace std;
class Point;
Point operator+(Point &a,Point &b);//要加声明
//##############################################
class Point
{

public:
void set(int a,int b)
{
x=a,y=b;
}
void print() const
{
cout<<"("<<x<<","<<y<<")\n";
}

friend Point operator+(const Point a,const Point b);
friend Point add( Point &a, Point &b);
private:
int x,y;
};
//---------------------------------------------------------
Point operator+( Point a,Point b)
{
Point s;
s.set(a.x+b.x,a.y+b.y);
return s;
}//---------------------------------------------------------
Point add( Point &a, Point &b)
{
Point s;
s.set(a.x+b.x,a.y+b.y);
return s;
}//===========================================================
void main()
{
Point a,b;
a.set(3,2);
b.set(1,5);
(a+b).print();
operator+(a,b).print();
add(a,b).print();
}

这是用VC++调试后出错信息:
--------------------Configuration: A_TEST - Win32 Debug--------------------
Compiling...
retry.cpp
E:\MATE\VC\A_TEST\retry.cpp(28) : error C2248: 'x' : cannot access private member declared in class 'Point'
E:\MATE\VC\A_TEST\retry.cpp(22) : see declaration of 'x'
E:\MATE\VC\A_TEST\retry.cpp(28) : error C2248: 'x' : cannot access private member declared in class 'Point'
E:\MATE\VC\A_TEST\retry.cpp(22) : see declaration of 'x'
E:\MATE\VC\A_TEST\retry.cpp(28) : error C2248: 'y' : cannot access private member declared in class 'Point'
E:\MATE\VC\A_TEST\retry.cpp(22) : see declaration of 'y'
E:\MATE\VC\A_TEST\retry.cpp(28) : error C2248: 'y' : cannot access private member declared in class 'Point'
E:\MATE\VC\A_TEST\retry.cpp(22) : see declaration of 'y'
E:\MATE\VC\A_TEST\retry.cpp(42) : error C2593: 'operator +' is ambiguous
E:\MATE\VC\A_TEST\retry.cpp(42) : error C2228: left of '.print' must have class/struct/union type
E:\MATE\VC\A_TEST\retry.cpp(43) : error C2668: '+' : ambiguous call to overloaded function
E:\MATE\VC\A_TEST\retry.cpp(43) : error C2228: left of '.print' must have class/struct/union type
执行 cl.exe 时出错.

A_TEST.exe - 1 error(s), 0 warning(s)
希望哪位高手经调试成功后,给我可行的代码,谢谢!!!
这是可行的代码,我运行通过...
#include <iostream.h>
//using namespace std;
class Point;

//##############################################
class Point
{

public:
void set(int a,int b)
{
x=a,y=b;
}
void print() const
{
cout<<"("<<x<<","<<y<<")\n";
}

friend Point operator+(const Point &a,const Point &b);
friend Point add( Point &a, Point &b);
private:
int x,y;
};
//---------------------------------------------------------
Point operator+(const Point &a,const Point &b)
{
Point s;
s.set(a.x+b.x,a.y+b.y);
return s;
}//---------------------------------------------------------
Point add( Point &a, Point &b)
{
Point s;
s.set(a.x+b.x,a.y+b.y);
return s;
}//===========================================================
void main()
{
Point a,b;
a.set(3,2);
b.set(1,5);
(a+b).print();
operator+(a,b).print();
add(a,b).print();
}

1。Point operator+(Point &a,Point &b);//要加声明
2。friend Point operator+(const Point a,const Point b);
3。Point operator+( Point a,Point b)
{...}
看上面的三行,为什么申明和定义不同?问题就出在这里。
根据习惯,应该这样:
1。根本不需要
2。3。friend Point operator+(const Point a,const Point b);
可以写成:
friend Point operator+(const Point &a,const Point &b)
但是建议不要用友元。

#include <iostream>
using namespace std;
class Point;
//##############################################
class Point
{

public:
void set(int a,int b) { x=a;y=b; }
void print() const { cout<<"("<<x<<","<<y<<")\n"; }
Point operator+( const Point &a);
friend Point add( const Point &a, const Point &b);
private:
int x,y;
};
//---------------------------------------------------------

Point Point::operator+(const Point &a)
{
Point s;
s.set(x+a.x,y+a.y);
return s;
}//---------------------------------------------------------
Point add(const Point &a,const Point &b)
{
Point s;
s.set(a.x+b.x,a.y+b.y);
return s;
}//===========================================================
void main()
{
Point a,b;
a.set(3,2);
b.set(1,5);
(a+b).print();
a.operator +(b).print();
add(a,b).print();
}

函数的声明和定义要统一啊,全部写成
Point operator+(const Point a,const Point b)

说明程序做什么用,加详细注释,这样的代码是没有人能看懂的。

眼都看花了。!!