'struct std :: pair <int, int>' does not have a name named 'serialize'
I am trying to integrate serialization in my code. However, I get the error "does not have a name with a name." The book I'm reading says that std :: pair does not need a header file to include, and it does not exist. How to fix this error? My code is as follows:
#include <iostream> //ofstream/ifstream #include <fstream> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> //stringstream #include <sstream> // #include <boost/serialization/complex.hpp> #include <boost/serialization/bitset.hpp> //#include <boost/serialization/ //BOOST_BINARY #include <boost/utility/binary.hpp> using namespace std; int main() { complex<double> c(1,0); bitset<3> b(BOOST_BINARY(101)); pair<int,int> p(1,2); string s; std::stringstream ss(s); boost::archive::text_oarchive oa(ss); oa<<c<<b<<p; { complex<double> c; bitset<3> b; pair<int,int> p; boost::archive::text_iarchive ia(ss); ia>>c>>b>>p; } return 0; } Add #include <boost/serialization/utility.hpp> to enable std::pair serialization.
Why std::pair provide a serialize function? The standard never talks about it. It is also incorrect to say that it does not need a header file: it really requires <utility>. However, it is probably already included in another header file that you are using.
You can provide your own serialize function. Boost already provides examples. In your case, you need to add the following code to main .
namespace boost { namespace serialization { template <class Archive, typename T1, typename T2> void serialize(Archive& ar, std::pair<T1, T2>& pr, const unsigned int version) { ar & pr.first; ar & pr.second; } } // namespace serialization } // namespace boost By the way, I don't see the need to use string s in your code. You can remove this line and just use:
std::stringstream ss;