从别处拷了段程序,在dev c++中报错,怎么修改?

dfczj 2007-06-01 05:28:04
#include<iostream>
using namespace std;
class point
{
public:
point(int xx,int yy){x=xx,y=yy;}
point(point &p);
~point(){cout<<"析构"<<endl;}
private:
int x;
int y;
};
point::point(point &p)
{
x=p.x;
y=p.y;
}
point fun2()
{
point A(1,2);
return A;
}
int main()
{
point B(1,2);
B=fun2();//这里报错,error: no matching function for call to`point::point(point)'

return 0;
}

在dev c++中报错,怎么修改?
...全文
364 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cangwu_lee 2007-06-03
  • 打赏
  • 举报
回复
ls, 这样也是错误, 怎么修改?

#include <cstdlib>
#include <iostream>

using namespace std;

class point{
public:
point(int xx, int yy){x=xx, y=yy;}
point(point &p);
~point(){cout<<"析构"<<endl;}

void operator=(point &p){
cout<<"operator 1\n";
x=p.x;
y=p.y;
}

const point& operator=(const point &p){
cout<<"operator 2\n";
if(this == &p){
return *this;
}
return *this;
}
private:
int x;
int y;
};

point::point(point &p){
x=p.x;
y=p.y;
}

const point fun2(){
point A(3, 4);
return A;
}


int main(int argc, char *argv[]){
point B(1,2);
B=fun2(); //这里报错,43 main.cpp no matching function for call to `point::point(const point)'

system("PAUSE");
return EXIT_SUCCESS;
}
gracefullee 2007-06-01
  • 打赏
  • 举报
回复
这个程序本身就有问题
定义了拷贝构造函数,却不重载赋值操作符
可能碰到机器和编译器不同,问题就暴露了吧!
monk0128 2007-06-01
  • 打赏
  • 举报
回复
我在VC下也没有错误。真奇怪。
Jacky0906 2007-06-01
  • 打赏
  • 举报
回复
在vc中不会报错,我也是这个程序了。还是用vc调试吧,因为如果在dev中不加system("pause")
你就看不到输出的结果。如果加上的话,dev就会停到system("pause"),这样你也看不到B的析构函数输出。
monk0128 2007-06-01
  • 打赏
  • 举报
回复
是不是缺乏copy constructor?
ayw215 2007-06-01
  • 打赏
  • 举报
回复
为何我的dev没有报错?

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧