gpt4 book ai didi

c++ - BGL adjacency_list::vertex_descriptor 评估为 void*

转载 作者:搜寻专家 更新时间:2023-10-31 02:20:45 24 4
gpt4 key购买 nike

boost::graph_traits<graph_type>::vertex_descriptor> vertex_map_type评估为 void* 使用 boost::listS 时对于 VertexListS

以下代码无法编译。但是使用 vecS而不是 listS有效。

#include <iostream>
#include <fstream>
#include <map>
#include <list>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adj_list_serialize.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_serialize.hpp>

typedef boost::adjacency_list<
boost::listS,
boost::listS, // << ------ vecS works but listS or setS doesn't !!
boost::bidirectionalS> graph_type;
typedef std::map<boost::uuids::uuid, typename boost::graph_traits<graph_type>::vertex_descriptor> vertex_map_type;

int main(){
graph_type graph;
vertex_map_type map;
std::ofstream stream("archive");
boost::archive::binary_oarchive archive(stream);
archive << graph;
archive << map;
return 0;
}

由于 void* 而无法编译

$ g++ graph.cpp -o graph -lboost_graph -lboost_serialization
In file included from /usr/include/boost/archive/detail/interface_oarchive.hpp:23:0,
from /usr/include/boost/archive/detail/common_oarchive.hpp:22,
from /usr/include/boost/archive/basic_binary_oarchive.hpp:34,
from /usr/include/boost/archive/binary_oarchive_impl.hpp:23,
from /usr/include/boost/archive/binary_oarchive.hpp:21,
from graph.cpp:9:
/usr/include/boost/archive/detail/oserializer.hpp: In instantiation of ‘static void boost::archive::detail::save_pointer_type<Archive>::invoke(Archive&, TPtr) [with TPtr = void*; Archive = boost::archive::binary_oarchive]’:
/usr/include/boost/archive/detail/oserializer.hpp:525:24: required from ‘void boost::archive::save(Archive&, T&) [with Archive = boost::archive::binary_oarchive; T = void* const]’
/usr/include/boost/archive/detail/common_oarchive.hpp:69:40: required from ‘void boost::archive::detail::common_oarchive<Archive>::save_override(T&, int) [with T = void* const; Archive = boost::archive::binary_oarchive]’
/usr/include/boost/archive/basic_binary_oarchive.hpp:75:7: required from ‘void boost::archive::basic_binary_oarchive<Archive>::save_override(const T&, int) [with T = void*; Archive = boost::archive::binary_oarchive]’
/usr/include/boost/archive/binary_oarchive_impl.hpp:51:9: required from ‘void boost::archive::binary_oarchive_impl<Archive, Elem, Tr>::save_override(T&, int) [with T = void* const; Archive = boost::archive::binary_oarchive; Elem = char; Tr = std::char_traits<char>]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:63:9: required from ‘Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = void* const; Archive = boost::archive::binary_oarchive]’
/usr/include/boost/serialization/nvp.hpp:79:9: [ skipping 52 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/boost/archive/detail/oserializer.hpp:525:24: required from ‘void boost::archive::save(Archive&, T&) [with Archive = boost::archive::binary_oarchive; T = const std::map<boost::uuids::uuid, void*>]’
/usr/include/boost/archive/detail/common_oarchive.hpp:69:40: required from ‘void boost::archive::detail::common_oarchive<Archive>::save_override(T&, int) [with T = const std::map<boost::uuids::uuid, void*>; Archive = boost::archive::binary_oarchive]’
/usr/include/boost/archive/basic_binary_oarchive.hpp:75:7: required from ‘void boost::archive::basic_binary_oarchive<Archive>::save_override(const T&, int) [with T = std::map<boost::uuids::uuid, void*>; Archive = boost::archive::binary_oarchive]’
/usr/include/boost/archive/binary_oarchive_impl.hpp:51:9: required from ‘void boost::archive::binary_oarchive_impl<Archive, Elem, Tr>::save_override(T&, int) [with T = std::map<boost::uuids::uuid, void*>; Archive = boost::archive::binary_oarchive; Elem = char; Tr = std::char_traits<char>]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:63:9: required from ‘Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = std::map<boost::uuids::uuid, void*>; Archive = boost::archive::binary_oarchive]’
graph.cpp:27:13: required from here
/usr/include/boost/archive/detail/oserializer.hpp:465:27: error: ‘void*’ is not a pointer-to-object type
register_type(ar, * t);
^
/usr/include/boost/archive/detail/oserializer.hpp:473:18: error: ‘void*’ is not a pointer-to-object type
save(ar, * t);

See it on coliru

最佳答案

没错。它确实评估为不透明的整数类型。这是设计使然并记录在案。

vecS 具有连续存储,但 listSsetS(等)具有迭代器稳定性。您可以选择您的应用程序所需的内容。

这意味着您需要一个 ID(BGL 算法为此使用 vertex_index_t 属性)。或者,只使用内置的序列化支持。

#include <boost/archive/binary_oarchive.hpp>
#include <boost/graph/adj_list_serialize.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_serialize.hpp>
#include <fstream>

typedef boost::adjacency_list<
boost::listS, boost::listS, boost::bidirectionalS,
boost::property<boost::vertex_index_t, boost::uuids::uuid>
> graph_type;

int main(){
graph_type graph;

std::ofstream stream("archive");
boost::archive::binary_oarchive archive(stream);
archive << graph;
}

关于c++ - BGL adjacency_list<listS, listS>::vertex_descriptor 评估为 void*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285096/

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