首页 > 解决方案 > 注意: 'Entity_c::Entity_c(const Entity_c&)' 被隐式删除,因为默认定义格式不正确:

问题描述

我知道这个问题已经被问过并得到了回答,但即使这样,我也无法弄清楚

    In file included from /usr/include/c++/9.2.0/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                     from /usr/include/c++/9.2.0/bits/allocator.h:46,
                     from /usr/include/c++/9.2.0/list:61,
                     from src/composants/List/List.hpp:14,
                     from src/composants/List/List.cpp:8:
    /usr/include/c++/9.2.0/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = Entity_c; _Args = {const Entity_c&}; _Tp = std::_List_node<Entity_c>]’:
    /usr/include/c++/9.2.0/bits/alloc_traits.h:482:2:   required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = Entity_c; _Args = {const Entity_c&}; _Tp = std::_List_node<Entity_c>; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<std::_List_node<Entity_c> >]’
    /usr/include/c++/9.2.0/bits/stl_list.h:633:33:   required from ‘std::__cxx11::list<_Tp, _Alloc>::_Node* std::__cxx11::list<_Tp, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const Entity_c&}; _Tp = Entity_c; _Alloc = std::allocator<Entity_c>; std::__cxx11::list<_Tp, _Alloc>::_Node = std::_List_node<Entity_c>]’
    /usr/include/c++/9.2.0/bits/stl_list.h:1907:10:   required from ‘void std::__cxx11::list<_Tp, _Alloc>::_M_insert(std::__cxx11::list<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {const Entity_c&}; _Tp = Entity_c; _Alloc = std::allocator<Entity_c>; std::__cxx11::list<_Tp, _Alloc>::iterator = std::_List_iterator<Entity_c>]’
    /usr/include/c++/9.2.0/bits/stl_list.h:1208:9:   required from ‘void std::__cxx11::list<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Entity_c; _Alloc = std::allocator<Entity_c>; std::__cxx11::list<_Tp, _Alloc>::value_type = Entity_c]’
    src/composants/List/List.cpp:57:29:   required from here
    /usr/include/c++/9.2.0/ext/new_allocator.h:145:20: error: use of deleted function ‘Entity_c::Entity_c(const Entity_c&)’
      145 |  noexcept(noexcept(::new((void *)__p)
          |                    ^~~~~~~~~~~~~~~~~~
      146 |        _Up(std::forward<_Args>(__args)...)))
          |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In file included from src/composants/List/List.cpp:9:
    src/composants/Entity/Entity.hpp:16:7: note: ‘Entity_c::Entity_c(const Entity_c&)’ is implicitly deleted because the default definition would be ill-formed:
       16 | class Entity_c {
          |       ^~~~~~~~
    src/composants/Entity/Entity.hpp:16:7: error: use of deleted function ‘sf::Mutex::Mutex(const sf::Mutex&)’
    In file included from src/composants/List/List.hpp:12,
                     from src/composants/List/List.cpp:8:
    /usr/include/SFML/System/Mutex.hpp:47:23: note: ‘sf::Mutex::Mutex(const sf::Mutex&)’ is implicitly deleted because the default definition would be ill-formed:
       47 | class SFML_SYSTEM_API Mutex : NonCopyable
          |                       ^~~~~
    /usr/include/SFML/System/Mutex.hpp:47:23: error: ‘sf::NonCopyable::NonCopyable(const sf::NonCopyable&)’ is private within this context
    In file included from /usr/include/SFML/System/Thread.hpp:32,
                     from src/composants/List/List.hpp:11,
                     from src/composants/List/List.cpp:8:
    /usr/include/SFML/System/NonCopyable.hpp:77:5: note: declared private here
       77 |     NonCopyable(const NonCopyable&);
          |     ^~~~~~~~~~~
    In file included from src/composants/List/List.cpp:9:
    src/composants/Entity/Entity.hpp:16:7: error: use of deleted function ‘sf::Mutex::Mutex(const sf::Mutex&)’
       16 | class Entity_c {

我不明白那些错误,但我仍然弄清楚我的代码的哪一部分正在抛出它:

 54 void List_c::operator++(int)
 55 {
 56     Entity_c *entity = new Entity_c;                                                   
 57     m_list.push_back(*entity);
 58 }

我不得不说,我真的不明白为什么这会抛出类似(据我所知)我试图调用的东西Entity_c::Entity_c(const Entity_c&)

Entity_c 定义如下:

 16 class Entity_c {                
 17 public:                         
 18     Entity_c();                 
 19     ~Entity_c();
 ...
 81 };

好吧,我可能根本不理解那个错误,所以请随时向我展示我的错误。

标签: c++compiler-errors

解决方案


主要线索是class SFML_SYSTEM_API Mutex : NonCopyable“使用已删除的函数 'sf::Mutex::Mutex(const sf::Mutex&)” - asf::Mutex不能被复制,并且push_back确实如此。

由于sf::Mutex无法复制,因此(默认情况下)有一个成员的东西也不能复制。

您可以Entity_c通过添加一个复制构造函数来实现可复制,该构造函数不会从原始互斥体复制互斥体,而是创建一个新的互斥体。
(或避免复制 - 见下文。)

您也不应该使用new- 您正在泄漏内存来创建对象。

要么这样做:

Entity_c entity;                                                   
m_list.push_back(entity);

或这个:

m_list.push_back(Entity_c{});

或者这个,根本不需要复制:

m_list.emplace_back();

(附带说明一下,使用 of++非常令人惊讶。我怀疑您是否经常这样做,以至于您需要非常简洁的语法 - 我会使用List_c::add_entity()或类似的东西。)


推荐阅读