为什么运行之后显示undefined reference to 'X::X(X const&)'

#include <iostream>

using namespace std;

class X
{
int i;
X(const X&);
public:
X (int ii=0) :i(ii) {};
X* clone() const
{
return new X(*this);
};
};

void f(const X& h)
{
X *t;
t=h.clone();
}

int main()
{
X n(9);
f(n);
return 0;
}

问一下,我这几行代码错在哪里了。
为什么运行之后显示undefined reference to 'X::X(X const&)'

我就是想在函数f()里面调用一下clone复制一个能被修改的局部拷贝
还有,题目中要求在X中声明一个私有类型的拷贝构造函数。加了那一行就不对了。

慕田峪7331174
浏览 128回答 2
2回答

繁星淼淼

用下边这种方式实现即可。用VS 2005是编译通过的using namespace std;#include <iostream>using namespace std;class X{int i;X(const X&);public:X (int ii=0) :i(ii) {};X* clone() const{X* tmp = new X;tmp->i = (*this).i;return tmp;};};void f(const X& h){X *t;t=h.clone();}int main(){X n(9);f(n);return 0;}

九州编程

f不是X的成员函数,所以不能调用clone。
打开App,查看更多内容
随时随地看视频慕课网APP