弱问 为什么编译通不过
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;
class B
{
public:
B(){ cout<<"construct"<< endl; };
B(B& b){cout << "copy sonstruct" <<endl;};
~B(){cout << "destructor" << endl;};
int i;
};
B trh(B b)
{
return b;
}
int main()
{
B b;
B c=trh(b);
}
问题点数:20、回复次数:4Top
1 楼OpenHero(开勇)回复于 2006-12-03 14:14:39 得分 0
main 加一个 return 0先Top
2 楼xiaohao824(尘飞扬)回复于 2006-12-03 14:14:45 得分 0
void main()Top
3 楼abblly(西边日出东边雨)回复于 2006-12-03 14:24:49 得分 4
B(B& b){cout << "copy sonstruct" <<endl;};
改为
B(const B& b){cout << "copy sonstruct" <<endl;};Top
4 楼owlling(owlman)回复于 2006-12-03 14:25:11 得分 16
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;
class B
{
public:
B(){ cout<<"construct"<< endl; };
B(const B& b)//参数应该是个B类的const引用
{cout << "copy sonstruct" <<endl;};
~B(){cout << "destructor" << endl;};
B& operator=(B b){}
int i;
};
B trh(B b)
{
return b;
}
int main()
{
B b;
B c=trh(b);//这儿调用的是拷贝构造函数,而拷贝构造函数的参数是个引用,
// 而你的trh返回的是个临时值,不可写的
// 你也许应该这样:把你的构造函数改成const引用,
}
==================================
欢迎访问我的个人主页:http://www.lingjie.net/
==================================Top




