gpt4 book ai didi

C++ std::allocator::allocate() 给我一个错误的指针

转载 作者:行者123 更新时间:2023-11-30 02:48:39 25 4
gpt4 key购买 nike

我正在用 C++ 重新创建一个链表,并且在重载 += 运算符时得到了一个错误的指针。我想我只是以错误的方式使用了分配器,但我可能是错的。

这里是上下文:

void MyLinkedList::operator+=( const std::string& s )
{
allocator<Node> a;
Node* n = a.allocate(1);

Node node(s);
(*n) = node;
if ( first == NULL )
first = n;
else
{
(*current).SetNext(n);
current = n;
}
}

其中 firstcurrent 属于 Node* 类型。提前致谢!

最佳答案

std::allocator分配原始未构造的存储。要使用存储,您必须使用 .construct()

a.construct(n, /* initializer */);

关于C++ std::allocator::allocate() 给我一个错误的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896182/

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