gpt4 book ai didi

c++ - 错误 C2039 : 'serialize' : is not a member of vector type

转载 作者:行者123 更新时间:2023-11-30 00:53:17 30 4
gpt4 key购买 nike

#include<iostream>
#include<boost/thread.hpp>
#include<vector>
#include<boost/asio.hpp>
#include <ctime>
#include <string>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>

using boost::asio::ip::tcp;

std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}

class maintain_overlay{

public:
maintain_overlay():thread_(boost::bind(&maintain_overlay::member_list_server, this))
{
thread_.join();
}
void member_list_server(){
boost::asio::io_service io_service;

tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));

for (;;)
{
tcp::socket socket(io_service);
acceptor.accept(socket);

member_list.push_back(socket.remote_endpoint());

std::string message = make_daytime_string();

std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa<<member_list;/////////////error comes because of this code

boost::system::error_code ignored_error;
boost::asio::write(socket, boost::asio::buffer(message),
boost::asio::transfer_all(), ignored_error);


}
}

private:
std::vector<tcp::endpoint> member_list;
boost::thread thread_;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & member_list;
}



};

我得到了错误

error C2039: 'serialize' : is not a member of 'boost::asio::ip::basic_endpoint<InternetProtocol>'

----编辑------

我认为我犯的一个错误是我试图序列化成员数据而不是整个对象实例...如果我想序列化 meber_list vector ,我应该如何以及在何处声明序列化方法?

最佳答案

问题是您正在尝试序列化 vector<tcp::endpoint>但是tcp::endpoint不可序列化。

如果您只想发送连接的 IP 地址和端口,则必须从 tcp::endpoint 中提取它们或编写您自己的序列化/反序列化方法。

您可以像这样以非侵入式方式重载 boost 的序列化函数:

namespace boost {
namespace serialization {

template<class Archive, class Protocol>
void save(Archive & ar, asio::ip::basic_endpoint<Protocol> & e, unsigned int version)
{
string ip = e.address().to_string();
short port = e.port();
ar & ip;
ar & port;
}
template<class Archive, class Protocol>
void load(Archive & ar, asio::ip::basic_endpoint<Protocol> & e, unsigned int version)
{
string ip;
short port;
ar & ip;
ar & port;
e = asio::ip::basic_endpoint<Protocol>(ip, port);

}

template<class Archive, class Protocol>
inline void serialize(Archive & ar, asio::ip::basic_endpoint<Protocol> & e, const unsigned int file_version) {
split_free(ar, e, file_version);
}

}
}

关于c++ - 错误 C2039 : 'serialize' : is not a member of vector type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16713471/

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