Error using boost_serialization
Can anyone tell me what is wrong with the following code: #include <iomanip> #include <iostream> #include <fstream> #include <string> #include <boost/archive/tmpdir.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/list.hpp> #include <boost/serialization/string.hpp> *class* ResData{ *friend* *class* boost::serialization::access; *friend* std::ostream& *operator*<<(std::ostream &os, const ResData& rD); *private*: std::string resName; int seqNum; int insNum; float sAS; *public*: ResData(); ResData(std::string, int, int); std::string getResName() const { *return* resName;} int getSeqNum() const { *return* seqNum;} int getInsNum() const { *return* insNum;} float getSAS() const { *return* sAS;} void setSAS(float sASP) { sAS = sASP;} *template*<*class* Archive> void serialize(Archive&, const unsigned int); }; std::ostream& *operator*<<(std::ostream& os, const ResData& rD){ *return* os << rD.getResName() << rD.getSeqNum() << rD.getInsNum() << rD.getSAS(); } int main(){ std::ofstream oFSBS("/home/Seb/Code/testData/results.dat"); boost::archive::text_oarchive oA(oFSBS); ResData temp("TEST", 1, 1); temp.setSAS(5555); oA << temp; oFSBS.close(); } I get the following error upon compilation: /usr/include/boost/archive/detail/oserializer.hpp: In function ‘void boost::archive::save(Archive&, T&) [with Archive = boost::archive::text_oarchive, T = ResData]’: /usr/include/boost/archive/basic_text_oarchive.hpp:78: instantiated from ‘void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = ResData, Archive = boost::archive::text_oarchive]’ /usr/include/boost/archive/detail/interface_oarchive.hpp:85: instantiated from ‘Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T = ResData, Archive = boost::archive::text_oarchive]’ test.cpp:54: instantiated from here /usr/include/boost/archive/detail/oserializer.hpp:566: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false I am running FC4 with gcc4.0.1 thanks, Sebastien
Sebastien Gerega wrote:
int main(){
std::ofstream oFSBS("/home/Seb/Code/testData/results.dat"); boost::archive::text_oarchive oA(oFSBS); ResData temp("TEST", 1, 1); temp.setSAS(5555); oA << temp;
You need to use const ResData& work_around = temp; oA << temp; For 1.33, Robert decided that you can't save non-const objects like this. Many objected, but we failed to persuade him. - Volodya
Ok thanks that helped. Now I got another problem. I can't put the std::ostream& *operator*<<(std::ostream& os, const ResData& rD){ *return* os << rD.getResName() << rD.getSeqNum() << rD.getInsNum() << rD.getSAS(); } in ResData.h because that results in multiple definition errors. But when I put it in ResData.cpp I get the following error: /usr/include/boost/serialization/access.hpp:109: undefined reference to `void ResData::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive&, unsi how do I get around this problem? thanks again, Sebastien Gerega Vladimir Prus wrote:
Sebastien Gerega wrote:
int main(){
std::ofstream oFSBS("/home/Seb/Code/testData/results.dat"); boost::archive::text_oarchive oA(oFSBS); ResData temp("TEST", 1, 1); temp.setSAS(5555); oA << temp;
You need to use
const ResData& work_around = temp; oA << temp;
For 1.33, Robert decided that you can't save non-const objects like this. Many objected, but we failed to persuade him.
- Volodya
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Sebastien Gerega wrote:
Ok thanks that helped. Now I got another problem. I can't put the
std::ostream& *operator*<<(std::ostream& os, const ResData& rD){ *return* os << rD.getResName() << rD.getSeqNum() << rD.getInsNum() << rD.getSAS(); }
in ResData.h because that results in multiple definition errors.
It should be inline.
But when I put it in ResData.cpp I get the following error: /usr/include/boost/serialization/access.hpp:109: undefined reference to `void
ResData::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive&,
unsi
The code you previously posted did not contain definition of ResData::serialize, only declaration. You need to define it, in the header file. - Volodya
participants (2)
-
Sebastien Gerega
-
Vladimir Prus