一段编译的小程序

djjlove_2008 2009-09-22 09:14:59
#include <iostream>
using namespace std;
class Test
{
public:
int a;
Test(int x)
{
a = x;
}
Test(Test &test)
{
cout<<"copy constructor"<<endl;
a = test.a;
}
};
void fun1(Test test)
{
cout<<"fun1()..."<<endl;
}
Test fun2(void)
{
Test t(2);
cout<<"fun2()..."<<endl;
return t;
}
int main()
{
Test t1(1);
Test t2 = t1;
cout<<"before fun1()..."<<endl;
fun1(t1);

Test t3 = fun2();
cout<<"after fun2()..."<<endl;

system("pause");
return 0;
}
这道程序我在VC++6.0上能编译通过,但在DEV-C++上却通不过,提示no matching function for call to "Text...",这道程序与最新标准不兼容吗?
...全文
73 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
djjlove_2008 2009-09-22
  • 打赏
  • 举报
回复
谢谢,编程是得考试蛮多问题的。。。呵呵。。。
hittlle 2009-09-22
  • 打赏
  • 举报
回复
回楼上的,我发现这和定义没定义=无关;将程序出错的那一行修改成

Test t3(fun2());

还是同样出错
它的拷贝构造函数也是定义了的,为什么会这样子;我认为是Dev-cpp本身的问题。
thy38 2009-09-22
  • 打赏
  • 举报
回复
你没有定义operator=,造成Test t3=fun2()无法执行。不要用VC6!

#include <iostream>
using namespace std;
class Test
{
public:
int a;
Test(int x) {
a = x;
}
Test(const Test& test) {
cout <<"copy constructor" <<endl;
a = test.a;
}
Test& operator= (const Test& t) {
cout <<"operator=" <<endl;
a = t.a;
}
};
void fun1(Test test)
{
cout <<"fun1()..." <<endl;
}
Test fun2(void)
{
Test t(2);
cout <<"fun2()..." <<endl;
return t;
}
int main()
{
Test t1(1);
Test t2 = t1;
cout <<"before fun1()..." <<endl;
fun1(t1);

Test t3 = fun2();
cout <<"after fun2()..." <<endl;

system("pause");
return 0;
}
liuwg9999 2009-09-22
  • 打赏
  • 举报
回复
where is the “Text”?
danxuezx 2009-09-22
  • 打赏
  • 举报
回复
程序中有Text吗?

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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