首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从“deleted function”中获取xmemory错误

从“deleted function”中获取xmemory错误
EN

Stack Overflow用户
提问于 2018-07-07 01:40:55
回答 1查看 858关注 0票数 1

因此,有一点背景知识;我正在使用Virtual Studio 2017,并学习C++。我刚刚下载了一个模板项目来工作,有一个特定的类,Enemy类(它是一个自上而下的射手类),它导致了一个超级令人困惑的错误;

Enemy::const(const Enemy &)':正在尝试引用已删除的函数

问题是,这个错误似乎只发生在我正在工作的计算机上。错误似乎发生在xmemory文件的这个类中(错误日志告诉我是这样的),特别是在倒数第四个函数中:

代码语言:javascript
复制
template<class _Alloc>
struct _Default_allocator_traits
{   // traits for std::allocator
using allocator_type = _Alloc;
using value_type = typename _Alloc::value_type;

using pointer = value_type *;
using const_pointer = const value_type *;
using void_pointer = void *;
using const_void_pointer = const void *;

using size_type = size_t;
using difference_type = ptrdiff_t;

using propagate_on_container_copy_assignment = false_type;
using propagate_on_container_move_assignment = true_type;
using propagate_on_container_swap = false_type;
using is_always_equal = true_type;

template<class _Other>
    using rebind_alloc = allocator<_Other>;

template<class _Other>
    using rebind_traits = allocator_traits<allocator<_Other>>;

_NODISCARD static _DECLSPEC_ALLOCATOR pointer allocate(_Alloc&, _CRT_GUARDOVERFLOW const size_type _Count)
    {   // allocate array of _Count elements
    return (static_cast<pointer>(_Allocate<_New_alignof<value_type>>(_Get_size_of_n<sizeof(value_type)>(_Count))));
    }

_NODISCARD static _DECLSPEC_ALLOCATOR pointer allocate(_Alloc&, _CRT_GUARDOVERFLOW const size_type _Count,
    const_void_pointer)
    {   // allocate array of _Count elements, with hint
    return (static_cast<pointer>(_Allocate<_New_alignof<value_type>>(_Get_size_of_n<sizeof(value_type)>(_Count))));
    }

static void deallocate(_Alloc&, const pointer _Ptr, const size_type _Count)
    {   // deallocate _Count elements at _Ptr
    // no overflow check on the following multiply; we assume _Allocate did that check
    _Deallocate<_New_alignof<value_type>>(_Ptr, sizeof(value_type) * _Count);
    }

template<class _Objty,
    class... _Types>
    static void construct(_Alloc&, _Objty * const _Ptr, _Types&&... _Args)
    {   // construct _Objty(_Types...) at _Ptr
    ::new (const_cast<void *>(static_cast<const volatile void *>(_Ptr)))
        _Objty(_STD forward<_Types>(_Args)...);
    }

template<class _Uty>
    static void destroy(_Alloc&, _Uty * const _Ptr)
    {   // destroy object at _Ptr
    _Ptr->~_Uty();
    }

_NODISCARD static size_type max_size(const _Alloc&) _NOEXCEPT
    {   // get maximum size
    return (static_cast<size_t>(-1) / sizeof(value_type));
    }

_NODISCARD static _Alloc select_on_container_copy_construction(const _Alloc& _Al)
    {   // get allocator to use
    return (_Al);
    }
};

有趣的是,这不会发生在我创建对象的时候;它发生在我把它们放在一个矢量对象中来存储它们的时候。任何帮助都将不胜感激!

附注:我给Visual Studio 2017贴上了标签,因为这个错误看起来和人们所描述的“常量向量对常量向量”很相似。据我所知,措辞是完全相同的。

EN

回答 1

Stack Overflow用户

发布于 2018-07-07 02:13:00

被删除的函数是复制构造函数。此错误消息表示不打算复制"Enemy“类。

代码语言:javascript
复制
Enemy(const Enemy&) = delete;

当您将对象添加到容器中时,STL向量的分配器将创建对象的副本。使用指针:

代码语言:javascript
复制
std::vector<Enemy*> enemy_ptrs;
enemy_ptrs.push_back(new Enemy());

上面的向量现在复制了对象所在的地址,而不是对象本身。只需记住删除它们或使用C++11 shared_ptrs。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51215399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档