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

标签 c++ pointers

我正在用 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/

相关文章:

c++ - 如何在 Arduino 中使用 C++ 成员函数作为中断处理程序?

c - ‘->’ 的无效类型参数

c++ - 错误 : 'hash_map' is not a member of 'std'

c++ - 多GPU模式下的tensorflow c++ SetDefaultDevice

c++ - 在 Mac OS 上使用 GCC 为 MS-DOS (DOSBox) 编译 C 程序

c++ - C++ 中的基本 OSC 教程?使用什么库?

c - 数组名称为指针,数组名称带有 & 运算符

c++ - auto_ptr 的大小是多少?

c - 如何更改 C 函数中传递给函数的指针所指向的内容?

qt - 调用deleteLater()后立即初始化指针