[Serialization] Undefined reference to... (header or compilation problem)
Hello, I wonder if anyone could help me with my problem. I'm trying to serialize my project's data. Beeing new to the boost libs I started with simple tutorial example. Today is the 3rd day I'm unable to launch a serialization of an object of the simpliest Vertex class containing 2 double values. The errors I get follow this patern: [mack/.../]$ g++ -Wall -o main test3.cpp /tmp/ccQkfdAS.o (.gnu.linkonce.t._ZN5boost7archive18text_oarchive_implINS0_13text_oarchiveEED2Ev+0x31): In function `boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::~text_oarchive_impl()': : undefined reference to `boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::~basic_text_oprimitive()' The problem is in line : boost::archive::text_oarchive oa (ofs); Any ideas? IMHO it's either compilation issue ( should I include any additional libs to cmd line?) or header order/lacking. To be sure I didn't miss anything else let me include the whole code: //CODE #include <iostream> #include <vector> #include <functional> #include <algorithm> #include <fstream> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/vector.hpp> //using namespace std; //using namespace boost::serialization; class Vertex { private: friend class boost::serialization::access; public: double x; double y; Vertex() {} // Serialization requires a default constructor Vertex(double newX, double newY) : x(newX), y(newY) {} double getX() const { return x; } double getY() const { return y; } void dump() { std::cout << x << " " << y << std::endl; } }; BOOST_IS_ABSTRACT(Vertex) namespace boost { namespace archive { template<class Archive> void serialize(Archive & ar, Vertex & vtx, const unsigned int version) { ar & vtx.x; ar & vtx.y; } } // namespace serialization } // namespace boost int main() { Vertex v1(1.5, 2.5); std::ofstream ofs("myfile.vtx"); boost::archive::text_oarchive oa(ofs); oa << v1; ofs.close(); return 0; } // END CODE I use g++ (GCC) 3.4.4 20050721 (Red Hat 3.4.4-2) Fedora Core Boost ver. 1.32 Thanks in advance for any feedback. Regards Misha
add the "const" Read the "rationale" section of the documentation for an explanation. Robert Ramey
int main() {
const Vertex v1(1.5, 2.5); // add "const" here std::ofstream ofs("myfile.vtx");
boost::archive::text_oarchive oa(ofs);
oa << v1; ofs.close();
return 0; }
participants (2)
-
mishaï¼ gildia.pl
-
Robert Ramey