chzhujian3 2019-02-22 17:27
浏览 487

vector中使用自定义allocator编译提示无法将参数<char>转换为<Other>,求帮助

代码片如下:

template<typename T>
struct secure_allocator :public std::allocator<T>
{
    typedef std::allocator<T> base;
    //size_type就是size_t,去平台化
    //for ( std::vector<int>::size_type i = 0; i < a.size(); i++ )
    //和for ( int i = 0; i < a.size(); i++ )后者可能报warning C4018: “<”: 有符号/无符号不匹配
    typedef typename base::size_type size_type;
    typedef typename base::difference_type  difference_type;
    typedef typename base::pointer pointer;
    typedef typename base::const_pointer const_pointer;
    typedef typename base::reference reference;
    typedef typename base::const_reference const_reference;
    typedef typename base::value_type value_type;


    secure_allocator() throw() {} // 构造函数必须实现
    secure_allocator(const secure_allocator& a) throw():base(a) {}
    ~secure_allocator() throw() {}

    //rebind 实现对不同类型使用同一种内存分配策略的要求
    //必须要重新定义,否则容器如 list, set, map 使用时作用域只能到达 std::allocator
    template<typename _Other>
    struct rebind
    {
        typedef secure_allocator<_Other> other;
    };
    void deallocate(T* p, std::size_t n)
    {
        if (p != NULL)
            memset(p, 0, sizeof(T) * n);
        allocator<T>::deallocate(p, n);
    }
};

后面变量定义:
std::vector<char, secure_allocator<char>> vch;

编译提示:
错误 C2664 “secure_allocator<_Other>::secure_allocator(const secure_allocator<_Other> &) throw()”: 无法将参数 1 从“secure_allocator”转换为“const secure_allocator<_Other> &” C
使用VS2017: c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\vector 554

请问如何修改啊?

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能