1

So, a little background; I'm using Virtual Studio 2017, and learning C++. I've just downloaded a template project to work on, and there's this one specific class, the Enemy class (its a top-down shooter kind of thing), which is causing a super confusing error;

Enemy::Enemy(const Enemy &)': attempting to reference a deleted function

Problem is, this error seems to only occur on the computer I'm working on. The error seems to occur in this class in the xmemory file (where the error log tells me it is), specifically in the fourth-to-last function:

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);
    }
};

Interestingly, this doesn't happen when I make the objects; It happens when I put them in a vector object to store them. Any help would be greatly appreciated!

p.s. I'm putting the tag of Visual Studio 2017 on because it seems this error is similar to some that people have described as "constant vectors vs. vector of constants". The wording, as far as I can tell, is exactly the same.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
Technetium
  • 11
  • 4
  • Did you try looking at this (https://stackoverflow.com/questions/31264984/c-compiler-error-c2280-attempting-to-reference-a-deleted-function-in-visual) ? – buchipper Jul 06 '18 at 17:43
  • How are you creating your objects? Did you do it by copying? – StoryTeller - Unslander Monica Jul 06 '18 at 18:06
  • I did copy a project, but that worked until I tried adding randomness and went about it the wrong way. See the new bottom of the question... Mistakes were made! – Technetium Jul 06 '18 at 18:09
  • 2
    These kinds of errors allow you to learn. The compiler wasn't broke, you were violating a specific contract and it was letting you know. If you can figure out why this was happening you will become a better programmer. `Don't put a random number generator in a header file` is the wrong take-away. – James Poag Jul 06 '18 at 18:49
  • Please put your solution in an answer, don't edit the question. – JHBonarius Jul 06 '18 at 18:53

1 Answers1

0

The deleted function is the copy constructor. This error message means that the "Enemy" class is not intended to be copied.

Enemy(const Enemy&) = delete;

The allocator for the STL vector will make copies of the objects when you add them to the container. Use pointers:

std::vector<Enemy*> enemy_ptrs;
enemy_ptrs.push_back(new Enemy());

The vector in the above now makes copies of the address where the object exists instead of the object itself. Just remember to delete them or use C++11 shared_ptrs.

jonrobm
  • 641
  • 5
  • 4