更新时间:2023-11-13 14:11:22
这不起作用,但是,如果我删除A的构造函数,则
可以工作。
This is not working, however, if I delete the constructor of A, it works.
当删除用户定义的构造函数时,编译器将隐式生成一个默认的构造函数。提供用户定义的构造函数时,编译器不会隐式生成默认的构造函数。
When you removed the user defined constructor, the compiler implicitly generates a default one. When you provide a user defined constructor, the compiler doesn't implicitly generate a default constructor.
std :: make_unique< T []>
要求使用默认构造函数。
std::make_unique<T[]>
requires the use of default constructors...
所以,提供一个,一切都应该正常工作
So, provide one, and all should work well
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <memory>
using namespace std;
class A {
string str;
public:
A() = default;
A(string _str): str(_str) {}
string getStr() {
return str;
}
};
int main()
{
unique_ptr<A[]> ptr = make_unique<A[]>(3);
}