gpt4 book ai didi

c++ - 如何在构造函数中使用 unique_ptr?

转载 作者:行者123 更新时间:2023-11-30 02:34:53 31 4
gpt4 key购买 nike

<分区>

在这里,我尝试在构造函数中使用 unique_ptr。它给出了以下错误:

函数“std::unique_ptr<_Ty, _Dx>::operator=(const std::unique_ptr<_Ty, _Dx>::_Myt &) [with _Ty=ABC, _Dx=std::default_delete]”(已声明at line 1487 of "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\memory") 不能被引用——它是一个被删除的函数

我怎样才能实现它?

StructCol.h

#include "stdafx.h"
#ifndef StructCol_H
#define StructCol_H

#include<string>
#include<memory>
using namespace std;

class ABCD
{
public:
std::unique_ptr<ABC> & n;

ABCD(std::unique_ptr<ABC> & n1) : n(n1)
{
n = n1;
}

void print()
{
cout << n->no << endl;
cout << n->text_c << endl;
cout << n->no_c << endl;
}
};

class ABC
{
public:
string test;
int no;
string text_c;
int no_c;

ABC()
{

}

ABC(string text_c1, int no_c1)
{
text_c = text_c1;
no_c = no_c1;
}

void print()
{
cout << test << endl;
cout << no << endl;
cout << text_c << endl;
cout << no_c << endl;
}
};

#endif

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com