cannot declare field ‘__gnu_cxx::__aligned_membuf<senseauto_memory::IMessageBase...

2025年10月24日 16:46 响应: 4.47秒 状态: success

🚨 错误信息

[ 16%] Building CXX object CMakeFiles/ic_nm_dataprovider_unittest.dir/src/data_provider/test_DataProvider.cpp.o [ 16%] Built target SQLiteCpp_cpplint In file included from /usr/include/c++/11/bits/shared_ptr_base.h:60, from /usr/include/c++/11/bits/shared_ptr.h:53, from /usr/include/c++/11/memory:77, from /usr/include/gtest/gtest.h:57, from /data/gerrit/workspace_GWM_repo/Platform/IC_NM_DataProvider/test/unittest/src/data_provider/test_DataProvider.cpp:5: /usr/include/c++/11/ext/aligned_buffer.h: In instantiation of ‘struct __gnu_cxx::__aligned_membuf<senseauto_memory::IMessageBase>::_Tp2’: /usr/include/c++/11/ext/aligned_buffer.h:56:33: required from ‘struct __gnu_cxx::__aligned_membuf<senseauto_memory::IMessageBase>’ /usr/include/c++/11/bits/stl_list.h:170:40: required from ‘struct std::_List_node<senseauto_memory::IMessageBase>’ /usr/include/c++/11/bits/list.tcc:73:19: required from ‘void std::__cxx11::_List_base<_Tp, _Alloc>::_M_clear() [with _Tp = senseauto_memory::IMessageBase; _Alloc = std::allocator<senseauto_memory::IMessageBase>]’ /usr/include/c++/11/bits/stl_list.h:500:9: required from ‘std::__cxx11::_List_base<_Tp, _Alloc>::~_List_base() [with _Tp = senseauto_memory::IMessageBase; _Alloc = std::allocator<senseauto_memory::IMessageBase>]’ /usr/include/c++/11/bits/stl_list.h:674:7: required from here /usr/include/c++/11/ext/aligned_buffer.h:54:25: error: cannot declare field ‘__gnu_cxx::__aligned_membuf<senseauto_memory::IMessageBase>::_Tp2::_M_t’ to be of abstract type ‘senseauto_memory::IMessageBase’ 54 | struct _Tp2 { _Tp _M_t; }; | ^~~~ In file included from /data/gerrit/workspace_GWM_repo/Platform/IC_NM_DataProvider/test/unittest/../../data_provider/include/public/DataProvider.h:13, from /data/gerrit/workspace_GWM_repo/Platform/IC_NM_DataProvider/test/unittest/src/data_provider/test_DataProvider.cpp:10: /data/gerrit/workspace_GWM_repo/Platform/IC_NM_DataProvider/test/unittest/../../memory_messages/include/public/message_base.h:14:7: note: because the following virtual functions are pure within ‘senseauto_memory::IMessageBase’: 14 | class IMessageBase | ^~~~~~~~~~~~ /data/gerrit/workspace_GWM_repo/Platform/IC_NM_DataProvider/test/unittest/../../memory_messages/include/public/message_base.h:19:25: note: ‘virtual senseauto_memory::MessageType senseauto_memory::IMessageBase::type()’ 19 | virtual MessageType type() = 0; | ^~~~ make[2]: *** [CMakeFiles/ic_nm_dataprovider_unittest.dir/build.make:261: CMakeFiles/ic_nm_dataprovider_unittest.dir/src/data_provider/test_DataProvider.cpp.o] Error 1 make[1]: *** [CMakeFiles/Makefile2:159: CMakeFiles/ic_nm_dataprovider_unittest.dir/all] Error 2 make: *** [Makefile:146: all] Error 2

🤖 AI解决方案

/usr/include/c++/11/ext/aligned_buffer.h:54:25: error: cannot declare field ‘__gnu_cxx::__aligned_membuf<senseauto_memory::IMessageBase>::_Tp2::_M_t’ to be of abstract type ‘senseauto_memory::IMessageBase’
   54 |       struct _Tp2 { _Tp _M_t; };
      |                         ^~~~
In file included from /data/gerrit/workspace_GWM_repo/Platform/IC_NM_DataProvider/test/unittest/../../memory_messages/include/public/message_base.h:14:
/data/gerrit/workspace_GWM_repo/Platform/IC_NM_DataProvider/test/unittest/../../memory_messages/include/public/message_base.h:19:25: note:   because the following virtual functions are pure within ‘senseauto_memory::IMessageBase’:
   19 |     virtual MessageType type() = 0;
      |                         ^~~~

错误翻译:
在 /usr/include/c++/11/ext/aligned_buffer.h 的 54 行,无法将成员变量 '_M_t' 声明为抽象类型 'senseauto_memory::IMessageBase' 的实例。
原因:'senseauto_memory::IMessageBase' 是一个抽象类(因为它包含纯虚函数 'virtual MessageType type() = 0;'),不能直接实例化。

根本原因
std::list(在内部使用了 __gnu_cxx::__aligned_membuf)尝试在内部创建一个 senseauto_memory::IMessageBase 类型的对象,但 IMessageBase 是一个抽象基类,不能被直接实例化。

解决方案
确保 senseauto_memory::IMessageBase 不是抽象类,或者提供一个具体的派生类来实例化。
如果 IMessageBase 不应该是抽象类,请移除 virtual MessageType type() = 0;= 0
如果 IMessageBase 应该是抽象类,那么不能将其直接存储在 std::list 中。应该存储指向其派生类对象的指针(例如 std::list>std::list)。

例如,使用智能指针来存储派生类对象:
修改 test_DataProvider.cpp 中使用 std::list 的地方,改为:

++
#include <memory> // 包含 <memory> 头文件

// ...

// 原来的可能是:
// std::list<senseauto_memory::IMessageBase> messageList;

// 修改为:
std::list<std::unique_ptr<senseauto_memory::IMessageBase>> messageList;

预防措施

  • 在定义接口类(包含纯虚函数)时,明确其作为抽象基类的意图,并注意不要在容器中直接存储该接口类的对象实例。

  • 使用智能指针(如 std::unique_ptrstd::shared_ptr)来管理多态对象的生命周期,尤其是在容器中存储指向基类指针的场景。