我正在用 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;
}
}
其中 first
和 current
属于 Node*
类型。
提前致谢!
最佳答案
std::allocator
分配原始未构造的存储。要使用存储,您必须使用 .construct()
。
a.construct(n, /* initializer */);
关于C++ std::allocator::allocate() 给我一个错误的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896182/