在C++为矢量编写自定义虚拟分配器时遇到问题

Trouble writing custom dummy allocator for vector in C++

本文关键字:分配器 虚拟 遇到 问题 自定义 C++      更新时间:2023-10-16

我正在尝试为vector<>编写一个简单的虚拟分配器,以便我可以将vector<>用作堆栈数组的包装器,如下所示:

#include <vector>
#include "stdio.h"
#include "stack_allocator.h"
using namespace std;
int main() {
    int buffer[100];
    vector<int, StackAllocator<int>> v((StackAllocator<int>(buffer, 100)));
    v.push_back(2);
    printf("%d", v[0]);
    v.pop_back();
}

我编写了StackAllocator类,但是我在Visual Studio中遇到了链接器错误,不幸的是,这些错误非常通用:

1> main.cpp
1> main.obj: error LNK2001: unresolved external symbol "public: void __cdecl std::StackAllocator<int,class std::allocator<int> >::destroy<int>(int *)" (??$destroy@H@?$StackAllocator@HV?$allocator@H@std@@@std@@QEAAXPEAH@Z)
1> main.obj: error LNK2001: unresolved external symbol "public: void __cdecl std::StackAllocator<int,class std::allocator<int> >::construct<int,int>(int *,int &&)" (??$construct@HH@?$StackAllocator@HV?$allocator@H@std@@@std@@QEAAXPEAH$$QEAH@Z)
1> main.obj: error LNK2001: unresolved external symbol "public: unsigned __int64 __cdecl std::StackAllocator<int,class std::allocator<int> >::max_size(void)const " (?max_size@?$StackAllocator@HV?$allocator@H@std@@@std@@QEBA_KXZ)
1> main.obj: error LNK2001: unresolved external symbol "public: void __cdecl std::StackAllocator<int,class std::allocator<int> >::deallocate(int *,unsigned __int64)" (?deallocate@?$StackAllocator@HV?$allocator@H@std@@@std@@QEAAXPEAH_K@Z)
1> main.obj: error LNK2001: unresolved external symbol "public: int * __cdecl std::StackAllocator<int,class std::allocator<int> >::allocate(unsigned __int64,void const *)" (?allocate@?$StackAllocator@HV?$allocator@H@std@@@std@@QEAAPEAH_KPEBX@Z)
1> main.obj: error LNK2001: unresolved external symbol "public: __cdecl std::StackAllocator<int,class std::allocator<int> >::StackAllocator<int,class std::allocator<int> >(int *,unsigned __int64,class std::allocator<int> const &)" (??0?$StackAllocator@HV?$allocator@H@std@@@std@@QEAA@PEAH_KAEBV?$allocator@H@1@@Z)
1> C:UsersmathuDesktopStackVectorx64ReleaseStackVector.exe : fatal error LNK1120: 6 unresolved externals

这是我的代码:

stack_allocator.h:

#pragma once
#include <functional>
namespace std {
    template <typename T, typename Allocator = std::allocator<T>>
    class StackAllocator {
    public:
        typedef typename allocator_traits<Allocator>::value_type value_type;
        typedef typename allocator_traits<Allocator>::pointer pointer;
        typedef typename allocator_traits<Allocator>::const_pointer const_pointer;
        typedef typename allocator_traits<Allocator>::size_type size_type;
        typedef typename allocator_traits<Allocator>::difference_type difference_type;
        typedef typename allocator_traits<Allocator>::const_void_pointer const_void_pointer;
        typedef typename Allocator::reference reference;
        typedef typename Allocator::const_reference const_reference;
        template<typename T2>
        struct rebind {
            typedef StackAllocator<T2> other;
        };
    private:
        size_t m_size;
        Allocator m_allocator;
        pointer m_begin;
        pointer m_end;
        pointer m_stack_pointer;
        bool pointer_to_internal_buffer(const_pointer p) const;
    public:
        StackAllocator(const Allocator& alloc = Allocator());
        StackAllocator(pointer buffer, size_t size, const Allocator& alloc = Allocator());
        template <typename T2>
        StackAllocator(const StackAllocator<T2, Allocator>& other);
        pointer allocate(size_type n, const_void_pointer hint = const_void_pointer());
        void deallocate(pointer p, size_type n);
        size_type capacity() const;
        size_type max_size() const noexcept;
        pointer address(reference x) const noexcept;
        const_pointer address(const_reference x) const noexcept;
        pointer buffer() const noexcept;
        template <typename T2, typename... Args>
        void construct(T2* p, Args&&... args);
        template <typename T2>
        void destroy(T2* p);
        template <typename T2>
        bool operator==(const StackAllocator<T2, Allocator>& other) const noexcept;
        template <typename T2>
        bool operator!=(const StackAllocator<T2, Allocator>& other) const noexcept;
    };
}

stack_allocator.cpp:

#include "stack_allocator.h"
namespace std {
#define AllocatorTemplate template <typename T, typename Allocator>
#define StackAlloc StackAllocator<T, Allocator>
    AllocatorTemplate StackAlloc::StackAllocator(const Allocator& alloc) :
        m_size(0),
        m_allocator(alloc),
        m_begin(nullptr),
        m_end(nullptr),
        m_stack_pointer(nullptr) {
    }
    AllocatorTemplate StackAlloc::StackAllocator(pointer buffer, size_t size, const Allocator& alloc) :
        m_size(size),
        m_allocator(alloc),
        m_begin(buffer),
        m_end(buffer + size),
        m_stack_pointer(buffer) {
    }
    AllocatorTemplate template <typename T2> StackAlloc::StackAllocator(const StackAllocator<T2, Allocator>& other) :
        m_size(other.m_size),
        m_allocator(other.m_allocator),
        m_begin(other.m_begin),
        m_end(other.m_end),
        m_stack_pointer(other.m_stack_pointer) {
    }
    AllocatorTemplate typename StackAlloc::size_type StackAlloc::capacity() const {
        return m_size;
    }
    AllocatorTemplate typename StackAlloc::pointer StackAlloc::allocate(size_type n, const_void_pointer hint) {
        if (n <= size_type(distance(m_stack_pointer, m_end))) {
            pointer result = m_stack_pointer;
            m_stack_pointer += n;
            return result;
        }
        else
            return m_allocator.allocate(n, hint);
    }
    AllocatorTemplate void StackAlloc::deallocate(pointer p, size_type n) {
        if (pointer_to_internal_buffer(p))
            m_stack_pointer -= n;
        else
            m_allocator.deallocate(p, n);
    }
    AllocatorTemplate typename StackAlloc::size_type StackAlloc::max_size() const noexcept {
        return m_size();
    }
    AllocatorTemplate template <typename T2, typename... Args> void StackAlloc::construct(T2* p, Args&&... args) {
        m_allocator.construct(p, forward<Args>(args)...);
    }
    AllocatorTemplate template <typename T2> void StackAlloc::destroy(T2* p) {
        m_allocator.destroy(p);
    }
    AllocatorTemplate typename StackAlloc::pointer StackAlloc::address(reference x) const noexcept {
        if (pointer_to_internal_buffer(addressof(x)))
            return addressof(x);
        else
            return m_allocator.address(x);
    }
    AllocatorTemplate typename StackAlloc::const_pointer StackAlloc::address(const_reference x) const noexcept {
        if (pointer_to_internal_buffer(addressof(x)))
            return addressof(x);
        else
            return m_allocator.address(x);
    }
    AllocatorTemplate typename StackAlloc::pointer StackAlloc::buffer() const noexcept {
        return m_begin;
    }
    AllocatorTemplate bool StackAlloc::pointer_to_internal_buffer(const_pointer p) const {
        return (!(less<const_pointer>()(p, m_begin)) && (less<const_pointer>()(p, m_end)));
    }
    AllocatorTemplate template <typename T2> bool StackAlloc::operator==(const StackAllocator<T2, Allocator>& other) const noexcept {
        return buffer() == other.buffer();
    }
    AllocatorTemplate template <typename T2> bool StackAlloc::operator!=(const StackAllocator<T2, Allocator>& other) const noexcept {
        return buffer() != other.buffer();
    }
}

我很抱歉发布了很多代码,但我真的不知道问题可能在哪里。任何人都可以发现问题所在吗?

因为模板类只能在头文件中实现。阅读以下内容:为什么模板只能在头文件中实现?