C ++处理双继承

时间:2017-06-15 08:36:27

标签: c++ inheritance serialization

我正在进行数据序列化,我遇到了以下问题。我有一个名为AttributeContainer的类,它可以包含不同类型的数据(点云,rgb图像等),我想在磁盘上序列化它。这个类是一个std :: map,其中键是一个字符串,值是一个指向可序列化对象的指针。到现在为止还挺好。我的问题是由于我想序列化的某个对象不仅继承了可序列化的类而且我不知道如何面对这种情况。这是代码:

typedef std::map<std::string, Serializable*> StringSerializablePtrMap;

struct Cloud3D : public Serializable, public std::vector<Point3D> {

. . .

}

class AttributeContainer : public StringSerializablePtrMap{

. . . 

void insertCloud(Cloud3D *c){
     insert(std::pair<string,Serializable*>("cloud",c));
}

. . .

};

我从编译器得到的错误是,基本上,它不知道如何将c转换为Serializable对象。

有人可以告诉我如何处理这个问题吗?

谢谢,

费德里科

回答下面的评论,这是编译器输出:

/home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp: In member function ‘void srrg_core_map_2::LocalMap3D::setCloud(srrg_core::Cloud3D*)’:
/home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:76:54: error: no matching function for call to ‘std::pair<std::__cxx11::basic_string<char>, srrg_boss::Serializable*>::pair(const char [6], srrg_core::Cloud3D*&)’
     insert (std::pair<string,Serializable*>("cloud",c));
                                                      ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/stl_tree.h:63,
                 from /usr/include/c++/5/map:60,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.h:3,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:206:9: note: candidate: template<class ... _Args1, long unsigned int ..._Indexes1, class ... _Args2, long unsigned int ..._Indexes2> std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>)
         pair(tuple<_Args1...>&, tuple<_Args2...>&,
         ^
/usr/include/c++/5/bits/stl_pair.h:206:9: note:   template argument deduction/substitution failed:
/home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:76:54: note:   mismatched types ‘std::tuple<_Args1 ...>’ and ‘const char [6]’
     insert (std::pair<string,Serializable*>("cloud",c));
                                                      ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/stl_tree.h:63,
                 from /usr/include/c++/5/map:60,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.h:3,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:155:9: note: candidate: template<class ... _Args1, class ... _Args2> std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>)
         pair(piecewise_construct_t, tuple<_Args1...>, tuple<_Args2...>);
         ^
/usr/include/c++/5/bits/stl_pair.h:155:9: note:   template argument deduction/substitution failed:
/home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:76:54: note:   cannot convert ‘"cloud"’ (type ‘const char [6]’) to type ‘std::piecewise_construct_t’
     insert (std::pair<string,Serializable*>("cloud",c));
                                                      ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/stl_tree.h:63,
                 from /usr/include/c++/5/map:60,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.h:3,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:150:12: note: candidate: template<class _U1, class _U2, class> constexpr std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&)
  constexpr pair(pair<_U1, _U2>&& __p)
            ^
/usr/include/c++/5/bits/stl_pair.h:150:12: note:   template argument deduction/substitution failed:
/home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:76:54: note:   mismatched types ‘std::pair<_T1, _T2>’ and ‘const char [6]’
     insert (std::pair<string,Serializable*>("cloud",c));
                                                      ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/stl_tree.h:63,
                 from /usr/include/c++/5/map:60,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.h:3,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:144:12: note: candidate: template<class _U1, class _U2, class> constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&)
  constexpr pair(_U1&& __x, _U2&& __y)
            ^
/usr/include/c++/5/bits/stl_pair.h:144:12: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/stl_pair.h:141:38: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
       template<class _U1, class _U2, class = typename
                                      ^
/usr/include/c++/5/bits/stl_pair.h:138:12: note: candidate: template<class _U2, class> constexpr std::pair<_T1, _T2>::pair(const _T1&, _U2&&)
  constexpr pair(const _T1& __x, _U2&& __y)
            ^
/usr/include/c++/5/bits/stl_pair.h:138:12: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/stl_pair.h:136:27: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
       template<class _U2, class = typename
                           ^
/usr/include/c++/5/bits/stl_pair.h:133:12: note: candidate: template<class _U1, class> constexpr std::pair<_T1, _T2>::pair(_U1&&, const _T2&)
  constexpr pair(_U1&& __x, const _T2& __y)
            ^
/usr/include/c++/5/bits/stl_pair.h:133:12: note:   template argument deduction/substitution failed:
/home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:76:54: note:   cannot convert ‘c’ (type ‘srrg_core::Cloud3D*’) to type ‘srrg_boss::Serializable* const&’
     insert (std::pair<string,Serializable*>("cloud",c));
                                                      ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/stl_tree.h:63,
                 from /usr/include/c++/5/map:60,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.h:3,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:128:17: note: candidate: constexpr std::pair<_T1, _T2>::pair(std::pair<_T1, _T2>&&) [with _T1 = std::__cxx11::basic_string<char>; _T2 = srrg_boss::Serializable*]
       constexpr pair(pair&&) = default;
                 ^
/usr/include/c++/5/bits/stl_pair.h:128:17: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/5/bits/stl_pair.h:127:17: note: candidate: constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = std::__cxx11::basic_string<char>; _T2 = srrg_boss::Serializable*]
       constexpr pair(const pair&) = default;
                 ^
/usr/include/c++/5/bits/stl_pair.h:127:17: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/5/bits/stl_pair.h:124:12: note: candidate: template<class _U1, class _U2, class> constexpr std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&)
  constexpr pair(const pair<_U1, _U2>& __p)
            ^
/usr/include/c++/5/bits/stl_pair.h:124:12: note:   template argument deduction/substitution failed:
/home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:76:54: note:   mismatched types ‘const std::pair<_T1, _T2>’ and ‘const char [6]’
     insert (std::pair<string,Serializable*>("cloud",c));
                                                      ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/bits/stl_tree.h:63,
                 from /usr/include/c++/5/map:60,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.h:3,
                 from /home/dede/workspaces/develop/src/srrg_core_map_2/src/srrg_core_map_2/local_map.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:112:26: note: candidate: constexpr std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = std::__cxx11::basic_string<char>; _T2 = srrg_boss::Serializable*]
       _GLIBCXX_CONSTEXPR pair(const _T1& __a, const _T2& __b)
                          ^
/usr/include/c++/5/bits/stl_pair.h:112:26: note:   no known conversion for argument 2 from ‘srrg_core::Cloud3D*’ to ‘srrg_boss::Serializable* const&’
/usr/include/c++/5/bits/stl_pair.h:108:26: note: candidate: constexpr std::pair<_T1, _T2>::pair() [with _T1 = std::__cxx11::basic_string<char>; _T2 = srrg_boss::Serializable*]
       _GLIBCXX_CONSTEXPR pair()
                          ^
/usr/include/c++/5/bits/stl_pair.h:108:26: note:   candidate expects 0 arguments, 2 provided
srrg_core_map_2/src/srrg_core_map_2/CMakeFiles/srrg_core_map_2_library.dir/build.make:134: recipe for target 'srrg_core_map_2/src/srrg_core_map_2/CMakeFiles/srrg_core_map_2_library.dir/local_map.cpp.o' failed
make[2]: *** [srrg_core_map_2/src/srrg_core_map_2/CMakeFiles/srrg_core_map_2_library.dir/local_map.cpp.o] Error 1
CMakeFiles/Makefile2:1627: recipe for target 'srrg_core_map_2/src/srrg_core_map_2/CMakeFiles/srrg_core_map_2_library.dir/all' failed
make[1]: *** [srrg_core_map_2/src/srrg_core_map_2/CMakeFiles/srrg_core_map_2_library.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j8 -l8" failed

1 个答案:

答案 0 :(得分:1)

对基类的隐式转换通常应该在不转换AFAIK的情况下工作,无论它是否是多重继承(如果同一个类通过不同的中间基类多次继承,则异常可能是这样)。

但是,无法转换为基类(隐式)的原因通常是继承的类是前向声明(我自己被这不止一次烧毁)。确保在

之前包含带有完整类声明的标题
void insertCloud(Cloud3D *c){
     insert(std::pair<string,Serializable*>("cloud",c));
}

因为要知道Cloud3D类确实是Serializable的实例,所以必须提供完整的类声明。如果该类仅是前向声明的(class Cloud3D;),则编译器不知道它是继承自Serializable并且不允许转换的事实。