';初始化';:无法从';zallocator<;char>';至';zallocator<;U>'; 我尝试尝试在项目实例文件中给出的C++中的OpenSSL示例,称为EnVieldCux.CXX,可以在TAR.GZ下载到该页上。

';初始化';:无法从';zallocator<;char>';至';zallocator<;U>'; 我尝试尝试在项目实例文件中给出的C++中的OpenSSL示例,称为EnVieldCux.CXX,可以在TAR.GZ下载到该页上。,c++,C++,当我尝试在Visual Studio 2017上运行该示例时。我收到以下错误:“初始化”:无法将文件xstring行1998中的“zallocator”转换为“zallocator” 以下是我得到的输出: 1>----构建已启动:项目:控制台应用程序3,配置:调试Win32------ 1> 控制台应用程序3.cpp 1> c:\program files(x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\

当我尝试在Visual Studio 2017上运行该示例时。我收到以下错误:
“初始化”:无法将文件xstring行1998中的“zallocator”转换为“zallocator”

以下是我得到的输出:

1>----构建已启动:项目:控制台应用程序3,配置:调试Win32------
1> 控制台应用程序3.cpp
1> c:\program files(x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xstring(1998):错误C2440:“初始化”:无法从“zallocator”转换为“zallocator”
1> 与
1>        [
1> U=std::\U容器\U代理
1>        ]
1> c:\program files(x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xstring(1998):注意:没有构造函数可以采用源类型,或者构造函数重载解析不明确
1> c:\program files(x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xstring(1997):注意:在编译类模板成员函数“void std::_String_alloc::_Free_proxy(void)”时
1> 与
1>        [
1> _Elem=char,
1> _Alloc=zallocator
1>        ]
1> c:\program files(x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xstring(1949):注意:请参阅正在编译的函数模板实例化“void std::_String_alloc::_Free_proxy(void)”的参考
1> 与
1>        [
1> _Elem=char,
1> _Alloc=zallocator
1>        ]
1> c:\program files(x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xstring(2066):注意:请参阅正在编译的类模板实例化“std::\u String\u alloc”的参考
1> 与
1>        [
1> _Elem=char,
1> _Alloc=zallocator
1>        ]
1> c:\users\xxx\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp(86):注意:请参阅正在编译的类模板实例化'std::basic_string'的参考
1> 已完成生成项目“ConsoleApplication3.vcxproj”--失败。
======生成:0成功,1失败,0最新,0跳过==========
为了简化操作,我下载了env-encrypt.cxx文件中的zallocator类:

template <typename T> 
struct zallocator
{
public:
    typedef T value_type;
    typedef value_type* pointer;
    typedef const value_type* const_pointer;
    typedef value_type& reference;
    typedef const value_type& const_reference;
    typedef std::size_t size_type;
    typedef std::ptrdiff_t difference_type;

    pointer address(reference v) const { return &v; }
    const_pointer address(const_reference v) const { return &v; }

    pointer allocate(size_type n, const void* hint = 0) {
        if (n > std::numeric_limits<size_type>::max() / sizeof(T))
            throw std::bad_alloc();
        return static_cast<pointer> (::operator new (n * sizeof(value_type)));
    }

    void deallocate(pointer p, size_type n) {
        OPENSSL_cleanse(p, n * sizeof(T));
        ::operator delete(p);
    }

    size_type max_size() const {
        return std::numeric_limits<size_type>::max() / sizeof(T);
    }

    template<typename U>
    struct rebind
    {
        typedef zallocator<U> other;
    };

    void construct(pointer ptr, const T& val) {
        new (static_cast<T*>(ptr)) T(val);
    }

    void destroy(pointer ptr) {
        static_cast<T*>(ptr)->~T();
    }

#if __cpluplus >= 201103L
    template<typename U, typename... Args>
    void construct(U* ptr, Args&&  ... args) {
        ::new (static_cast<void*> (ptr)) U(std::forward<Args>(args)...);
    }

    template<typename U>
    void destroy(U* ptr) {
        ptr->~U();
    }
#endif
};
模板
结构定位器
{
公众:
类型定义T值_类型;
typedef值\u type*指针;
类型定义常量值\u类型*常量指针;
类型定义值\u类型和参考;
类型定义常量值\u类型和常量参考;
typedef std::size\u t size\u type;
typedef std::ptrdiff_t difference_type;
指针地址(引用v)常量{return&v;}
常量指针地址(常量引用v)常量{return&v;}
指针分配(大小\类型n,常量void*hint=0){
如果(n>std::numeric_limits::max()/sizeof(T))
抛出std::bad_alloc();
返回静态类型转换(::运算符new(n*sizeof(value类型));
}
无效解除分配(指针p,大小\类型n){
OPENSSL_clean(p,n*sizeof(T));
::运算符删除(p);
}
大小\类型最大大小()常量{
返回std::numeric_limits::max()/sizeof(T);
}
模板
结构重新绑定
{
其他类型定位器;
};
无效构造(指针ptr、常量T和val){
新的(静态_-cast(ptr))T(val);
}
无效销毁(指针ptr){
静态_cast(ptr)->~T();
}
#如果uu cplululus>=201103L
模板
无效构造(U*ptr、参数和…参数){
::new(static_cast(ptr))U(std::forward post我遇到另一个异常:“zallocater::zallocater”:没有合适的默认构造函数可用

我尝试添加没有如下参数的构造函数:`

template<class U> zallocator() noexcept { }
模板zallocator()noexcept{}
`,但仍会出现相同的错误。请查看、和CPPAT引用。最后一个链接有以下最小c++17分配器示例:

template <class T>
struct Mallocator {
  typedef T value_type;
  Mallocator() = default;
  template <class U> constexpr Mallocator(const Mallocator<U>&) noexcept {}
  [[nodiscard]] T* allocate(std::size_t n) {
    if(n > std::size_t(-1) / sizeof(T)) throw std::bad_alloc();
    if(auto p = static_cast<T*>(std::malloc(n*sizeof(T)))) return p;
    throw std::bad_alloc();
  }
  void deallocate(T* p, std::size_t) noexcept { std::free(p); }
};
template <class T, class U>
bool operator==(const Mallocator<T>&, const Mallocator<U>&) { return true; }
template <class T, class U>
bool operator!=(const Mallocator<T>&, const Mallocator<U>&) { return false; }
还有几个施工人员:

constexpr zallocator() noexcept = default;
template< class U>
constexpr zallocator(const zallocator<U>& other) noexcept
{
}
constexpr zallocator()noexcept=默认值;
模板
constexpr zallocator(const zallocator和其他)无例外
{
}

在尝试修复构造函数错误时,您编写了:

template<class U> zallocator() noexcept { }
模板zallocator()noexcept{}

但不是默认的构造函数。默认构造函数不应该模板化。< /P>谢谢。OpenSSL的C++示例是C++ 11标准,Visual C++编译器我可以模仿不低于14。使用您的建议允许编译它。

template <typename U>
friend bool operator==(const zallocator<T> & a, const zallocator<U> & b)
{
    return true;
}
template <typename U>
friend bool operator!=(const zallocator<T> & a, const zallocator<U> & b)
{
    return false;
}
constexpr zallocator() noexcept = default;
template< class U>
constexpr zallocator(const zallocator<U>& other) noexcept
{
}
template<class U> zallocator() noexcept { }