简体   繁体   English

c++ 中的分配器错误

[英]mistake with allocators in c++

namespace Test
{
    template <typename DataType, typename Allocator = std::allocator<DataType>>
    class List 
    {
    private:
        struct Node
        {
            Node* next;
            DataType Data;
            Node(DataType Data,Node* next = nullptr);
        };

        typename Allocator::template rebind<Node*>::other allocator;
        Node* head;
        int size;

    public :
        List();
        ~List();

        void PushFront(DataType Data);
        //void Insert(DataType Data, size_t index);
        //void DeleteByIndex(size_t index);

        template <typename DataType, typename Allocator = std::allocator<DataType>>
        friend std::ostream& operator<<(std::ostream& out, Test::List<DataType, Allocator> list);
    };

    template<typename DataType, typename Allocator = std::allocator<DataType>>
    std::ostream& operator<<(std::ostream& out, Test::List<DataType, Allocator> list)
    {
        typename decltype(list)::Node* current = list.head;
        for (size_t i = 0; i < list.size; ++i)
        {
            out << current.Data << " ";
            current = current->next;
        }
    }
    template<typename DataType, typename Allocator>
    inline List<DataType, Allocator>::Node::Node(DataType Data,Node* next) :
        Data(Data),
        next(next)
    {}

    template <typename DataType, typename Allocator>
    Test::List<DataType, Allocator>::List() :
        head(nullptr),
        size(NO_SIZE)
    {}

    template <typename DataType, typename Allocator>
    Test::List<DataType, Allocator>::~List()
    {
        Node* help = head->next;
        for (size_t i = 0; i < size; ++i)
        {
            allocator.destroy(head);
            allocator.deallocate(head, 1);
            head = help;
            help = help->next;
        }
    }

    template <typename DataType, typename Allocator>
    void Test::List<DataType, Allocator>::PushFront(DataType Data)
    {
        Node* newHead = allocator.allocate(1);
        allocator.construct(newHead, Data, head);
        head = newHead;
    }

in main i am trying to make this List主要我正在尝试制作这个列表

int main()
{
    Test::List<int> l;
    l.PushFront(10);
    l.PushFront(20);
}

and i get errors:我得到错误:

C2664 "void std::allocator<_Other>::deallocate(_Ty *const,const size_t)": impossuble to convert first argument from "Test::List>::Node *" in "_Ty *const " in this string(allocator.deallocate(head, 1);) C2664“void std::allocator<_Other>::deallocate(_Ty *const,const size_t)”:无法从“_Ty *const”中的“Test::List>::Node *”转换第一个参数( allocator.deallocate(head, 1);)

C2440 initialization: impossible convert "_Ty *" in "Test::List>::Node " in this string(Node newHead = allocator.allocate(1);) C2440 初始化:不可能在此字符串中的“Test::List>::Node”中转换“_Ty *”(Node newHead = allocator.allocate(1);)

how to fix this(errors was translated by me, sorry if have mistakes)如何解决此问题(错误由我翻译,如有错误,请见谅)

Try changing尝试改变

typename Allocator::template rebind<Node*>::other allocator;

to

typename Allocator::template rebind<Node>::other allocator;

The pointer is implicit.指针是隐式的。

Also you should use std::allocator_traits together with an allocator instead of using the typedefs and functions of an allocator directly.此外,您应该将std::allocator_traits与分配器一起使用,而不是直接使用分配器的 typedef 和函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM