gpt4 book ai didi

c++ - 注意: ‘Entity_c::Entity_c(const Entity_c&)’ is implicitly deleted because the default definition would be ill-formed:

转载 作者:行者123 更新时间:2023-12-02 11:10:07 26 4
gpt4 key购买 nike

我知道这个问题已经被提出并回答了,但是即使如此,我也无法弄清楚

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

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

最佳答案

主要线索是class SFML_SYSTEM_API Mutex : NonCopyable和“使用删除的功能‘sf::Mutex::Mutex(const sf::Mutex&)” –不能复制sf::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();

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

关于c++ - 注意: ‘Entity_c::Entity_c(const Entity_c&)’ is implicitly deleted because the default definition would be ill-formed:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59288140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com