C++;std::allocator::allocate()给了我一个错误的指针 < >我正在重新创建C++中的链表,并且在重载+=运算符时得到一个坏指针。我想我只是以错误的方式使用了分配器,但我可能是错的
以下是上下文:C++;std::allocator::allocate()给了我一个错误的指针 < >我正在重新创建C++中的链表,并且在重载+=运算符时得到一个坏指针。我想我只是以错误的方式使用了分配器,但我可能是错的,c++,pointers,C++,Pointers,以下是上下文: 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)
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;
}
}
void MyLinkedList::operator+=(常量std::string&s)
{
分配器a;
节点*n=a.allocate(1);
节点;
(*n)=节点;
if(first==NULL)
第一个=n;
其他的
{
(*当前).SetNext(n);
电流=n;
}
}
其中第一个
和当前
属于节点类型*
。
提前谢谢 分配未构建的原始存储。要使用存储,必须使用.construct()
你犯了什么错误?谢谢!现在,在设置
节点中当前所指向的节点的节点*下一个的旧指针时遇到问题。它告诉我在那个书写位置有一个访问冲突。我在谷歌上搜索了一下,似乎我可能在只读内存中创建了我的节点。我有没有,如果有,我该如何把它放在可写内存中?
a.construct(n, /* initializer */);