新手请教一个问题,期望指导!谢谢
humin
2007-04-07
#include<iostream>
using namespace std; class Point { public: Point(int xx=0,int yy=0){x=xx;y=yy;} Point(point &p); int getx(){return x;} int gety(){return y;} private: int x,y; }; Point::Point(point &p) {x=p.x ;y=p.y;cout<<"调用拷贝构造函数"<<endl;} void fun1(Point p) {cout<<p.getx()<<endl;} Point fun2() { Point A(1,2); return A;} int main() { Point(4,5); Point B(A); cout<<B.getx()<<endl; fun1(B); B=fun2(); cout<<B.getx()<<endl; } 这段程序有错吗?为何提示A为定义呢? |
|
wzgme
2007-04-07
A没有在main()中定义啊??fun2()中是局部变量啊。另外拷贝函数也写不对啊
可以这样改: int main() { Point A(4,5); Point B(A); cout<<B.getx()<<endl; fun1(B); B=fun2(); cout<<B.getx()<<endl; cin.get(); } |
|
小帽子
2007-05-11
void fun1(Point p) 应改为void fun1(Point &p)好些吧
|