引用 Well, how does the custom deleter of std::unique_ptr work?
构造函数
std::unique_ptr<ErrorHandling> error_;
RecursiveDescentParser::RecursiveDescentParser(std::string inputStream, bool fileService,
boost::optional<std::string> filePathName, std::ofstream &writer){
if (fileService == true){
error_(new ErrorHandling(fileService, writer)); <---- compiler error
}
else{
error_(new ErrorHandling(fileService, std::ofstream())); <---- compiler error
}
}
编译错误
Error 1 error C2247: 'std::default_delete<_Ty>::operator ()' not accessible because 'std::unique_ptr<_Ty>' uses 'private' to inherit from 'std::_Unique_ptr_base<_Ty,_Dx,_Empty_deleter>'
错误原因描述here .
我决定自 'std::default_delete<_Ty>::operator ()
是private
因为子类(在本例中为 std::unique_ptr
)指定了 private inheritance
我会写我自己的自定义删除器。问题是我对成功的语法和符号太不舒服了。
最佳答案
这一行
error_(new ErrorHandling(fileService, writer));
是一个错误,因为 unique_ptr
没有 operator()
。错误消息有点误导,因为它的基类似乎有一个(但幸运的是私有(private)的)。
你可能有意
error_.reset(new ErrorHandling(fileService, writer));
这使得 unique_ptr
拥有一个新对象。
关于c++ - std::unique_ptr 自定义删除器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15884034/