为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C++
慕田峪7331174 2023-04-24 09:05:45
#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中声明一个私有类型的拷贝构造函数。加了那一行就不对了。
查看完整描述

2 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

用下边这种方式实现即可。用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;
}


查看完整回答
反对 回复 2023-04-27
?
九州编程

TA贡献1785条经验 获得超4个赞

f不是X的成员函数,所以不能调用clone。

查看完整回答
反对 回复 2023-04-27
  • 2 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信