在VS 2017中,朋友通过具有私有析构函数的结构的unique_ptr向量进行迭代失败
Iterating through vector of unique_ptr of a struct with private destructor, by a friend, fails in VS 2017
这可能是一个答案很简单的问题,但我尝试了谷歌结果提供的所有解决方案,但无法在VS 2017中解决这个问题。我有一个带有私有析构函数的结构B
。我有另一个结构A
,它是结构B
的朋友,并试图遍历结构B
的唯一指针向量。但我一直收到这个错误:
严重性代码描述项目文件行禁止显示状态错误C2248"ProjectNamespace::NwSpec::~NwSpec":无法访问类"ProjectNamespace::NwSpec"TestProject c:\program files(x86(\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\memory 2055 中声明的私有成员
任何指针都将不胜感激。谢谢
struct A
{
//functions
std::vector<std::unique_ptr<NwSpec>> List;
bool function()const;
};
struct NwSpec
{
char* Path;//Assume Path is initialized and set with a string.
private:
~NwSpec();
friend struct A;
};
bool A::function()const
{
for (uint32_t i = 0 ; i < List.size(); i++)
{
OutputDebugStringA(List[i]->Path);// Error C2248
}
// I used iterator to the vector, but getting same error.
// I used for(const auto& k : List) but getting same access error.
// NwSpec is visible to A and within ProjectNamespace
}
您需要创建一个专门的类型来负责NwSpec对象的删除,并将其提供给std::unique_ptr
:
struct NwSpec;
struct A {
private:
struct NwSpecDeleter {
//Note that we're only declaring the function, and not defining it
void operator()(NwSpec * ptr) const;
};
public:
typedef std::unique_ptr<NwSpec, NwSpecDeleter> ptr_type;
std::vector<ptr_type> List;
bool function()const;
};
struct NwSpec {
char* Path;//Assume Path is initialized and set with a string.
private:
~NwSpec();
friend struct A;
};
NwSpec::~NwSpec() {}
void A::NwSpecDeleter::operator()(NwSpec * ptr) const {
delete ptr;
}
int main() {
A a;
a.List.emplace_back(new NwSpec());
auto ptr = a.List.back().release();
//This will not compile, as intended
//delete ptr;
//Nor will this:
//ptr->~NwSpec();
//But this will compile, because the original unique_ptr is perfectly capable of deleting objects:
a.List.back().reset(ptr);
a.List.back().reset();//Object is successfully deleted
a.List.erase(a.List.begin(), a.List.end());
a.List.emplace_back(new NwSpec());
//This also compiles
a.List.erase(a.List.begin(), a.List.end());
return 0;
}
此代码可以在此处进行测试。
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 为什么 std::unique 不调用 std::sort?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- C++:'unique vector'数据结构