Skip to main content
GameDev.net gamedev.net
🔒 Locked

Need some Boost assistance please [resolved]

Started by Drew_Benton Mar 8, 2006 at 3:32 PM 8 replies 5.8k views
Original Post
Drew_Benton
Drew_Benton
I'm trying to use the Boost Serialization class, but I just can't get it to work, not my code, not their code, nor any other code I've tried online. I'm pretty sure I built it right, I spent a while figuring it out and built it all. I tried my usual Google searching for any help, but haven't been able to come up with anything. I am highly disappointed with Boost right now, everyone seems to reccomend it, yet I have not found a single tutorial/example on their site that works as is. Anyways, I'm about to just do it all myself but wanted to see if the problem was me or just Boost in general, I am quite disappointed right now. Errors:

------ Build started: Project: ProjectC, Configuration: Debug Win32 ------

Compiling...
Main.cpp
c:\Boost\include\boost\archive\detail\oserializer.hpp(131) : error C2027: use of undefined type 'boost::serialization::extended_type_info_null<T>'
        with
        [
            T=Engine
        ]
        c:\Boost\include\boost\archive\detail\oserializer.hpp(131) : see reference to class template instantiation 'boost::serialization::extended_type_info_null<T>' being compiled
        with
        [
            T=Engine
        ]
        c:\Boost\include\boost\archive\detail\oserializer.hpp(128) : while compiling class-template member function 'bool boost::archive::detail::oserializer<Archive,T>::is_polymorphic(void) const'
        with
        [
            Archive=boost::archive::text_oarchive,
            T=Engine
        ]
        c:\Boost\include\boost\archive\detail\oserializer.hpp(264) : see reference to class template instantiation 'boost::archive::detail::oserializer<Archive,T>' being compiled
        with
        [
            Archive=boost::archive::text_oarchive,
            T=Engine
        ]
        c:\Boost\include\boost\archive\detail\oserializer.hpp(263) : while compiling class-template member function 'void boost::archive::detail::save_non_pointer_type<Archive,T>::save_standard::invoke(Archive &,const Engine &)'
        with
        [
            Archive=boost::archive::text_oarchive,
            T=Engine
        ]
        c:\Boost\include\boost\archive\detail\oserializer.hpp(322) : see reference to class template instantiation 'boost::archive::detail::save_non_pointer_type<Archive,T>::save_standard' being compiled
        with
        [
            Archive=boost::archive::text_oarchive,
            T=Engine
        ]
        c:\Boost\include\boost\archive\detail\oserializer.hpp(310) : while compiling class-template member function 'void boost::archive::detail::save_non_pointer_type<Archive,T>::invoke(Archive &,const Engine &)'
        with
        [
            Archive=boost::archive::text_oarchive,
            T=Engine
        ]
        c:\Boost\include\boost\archive\detail\oserializer.hpp(536) : see reference to class template instantiation 'boost::archive::detail::save_non_pointer_type<Archive,T>' being compiled
        with
        [
            Archive=boost::archive::text_oarchive,
            T=Engine
        ]
        c:\Boost\include\boost\archive\basic_text_oarchive.hpp(78) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,T &)' being compiled
        with
        [
            Archive=boost::archive::text_oarchive,
            T=const Engine
        ]
        c:\Boost\include\boost\archive\detail\interface_oarchive.hpp(78) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<T>(T &,int)' being compiled
        with
        [
            Archive=boost::archive::text_oarchive,
            T=const Engine
        ]
        c:\Documents and Settings\Drew Benton\My Documents\Visual Studio Projects\ProjectC\Main.cpp(65) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator <<<const Engine>(T &)' being compiled
        with
        [
            Archive=boost::archive::text_oarchive,
            T=const Engine
        ]
c:\Boost\include\boost\archive\detail\oserializer.hpp(131) : error C2146: syntax error : missing ';' before identifier 'typex'
c:\Boost\include\boost\archive\detail\oserializer.hpp(131) : error C2065: 'typex' : undeclared identifier

Build log was saved at "file://c:\Documents and Settings\Drew Benton\My Documents\Visual Studio Projects\ProjectC\Debug\BuildLog.htm"
ProjectC - 3 error(s), 0 warning(s)


---------------------- Done ----------------------

    Build: 0 succeeded, 1 failed, 0 skipped


Code:

#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>

#include <boost/serialization/utility.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/split_member.hpp>

class Engine
{
private:
	int windowWidth;
	int windowHeight;

	friend class boost::serialization::access;
    // When the class Archive corresponds to an output archive, the
    // & operator is defined similar to <<.  Likewise, when the class Archive
    // is a type of input archive the & operator is defined similar to >>.
    template<class Archive>
    void save(Archive & ar, const unsigned int version)
    {
        ar & windowWidth;
        ar & windowHeight;
    }

	template<class Archive>
    void load(Archive & ar, const unsigned int version)
    {
        ar & windowWidth;
        ar & windowHeight;
    }

	BOOST_SERIALIZATION_SPLIT_MEMBER()

public:
	Engine()
	{
	}

	~Engine()
	{
	}

	//friend 
};

BOOST_CLASS_VERSION(Engine, 1)

int main(int argc, char* argv[])
{
	const Engine myEngine;

	// create and open a character archive for output
	std::ofstream ofs("filename.txt");

	boost::archive::text_oarchive oa(ofs);
	
	// write class instance to archive
	oa << myEngine;

	// Close the file
	ofs.close();

	return 0;
}


Oh yea, I even tried to use their entire example but it did not work either. I'm using VS 7.1/C++ and did the build of boost myself following their guide. [sad] Any ideas? I'd appreciate any help before I give up on this. Thanks for your time. [Edited by - Drew_Benton on March 9, 2006 6:00:39 PM]
Baraclese
Baraclese
Put archive includes before serialization includes:

#include
#include
#include
....

then add const qualifier to save function:

template
void save(Archive & ar, const unsigned int version) const
{...}


This should make it compile.

Also there will be speed improvements for binary serialization in the upcoming boost release. Text archives carry the usual overhead of iostream text io.

Baraclese
Drew_Benton
Drew_Benton
Oh, wow, thanks! It does compile but it crashes. I messed around a bit and found it crashed on the << operation, so I just browsed though the boost::archive::text_oarchive class using intellisense and saw the save/load functions that take a templated parameter. I implemented the << and >> for the class and it works now when I use the save/load, but not the << and >>, figures, eh?

Now my question is, how the heck would I have known to add const to those functions as well as order the header files that way? On their demo example they show utility above the archives and when I tried it that way after adding the const, it didn't work again.

I think I'm just going to use my own stuff. I'm worried about stuff like this thoughout the rest of the boost docs and this all seems to be more of a hassal than a solution. Anyways, here's my final working code after a few modifications, thanks again!

#include <string>#include <iomanip>#include <iostream>#include <fstream>#include <boost/archive/text_iarchive.hpp>#include <boost/archive/text_oarchive.hpp>#include <boost/serialization/utility.hpp>#include <boost/serialization/base_object.hpp>#include <boost/serialization/list.hpp>#include <boost/serialization/string.hpp>#include <boost/serialization/version.hpp>#include <boost/serialization/split_member.hpp>class Engine{private:		friend class boost::serialization::access;    // When the class Archive corresponds to an output archive, the    // & operator is defined similar to <<.  Likewise, when the class Archive    // is a type of input archive the & operator is defined similar to >>.    template<class Archive>    void save(Archive & ar, const unsigned int version) const    {        ar & windowWidth;        ar & windowHeight;    }	template<class Archive>    void load(Archive & ar, const unsigned int version) const    {        ar & windowWidth;        ar & windowHeight;    }	BOOST_SERIALIZATION_SPLIT_MEMBER()public:	int windowWidth;	int windowHeight;	Engine()	{		this->windowWidth = 0;		this->windowHeight = 0;	}	~Engine()	{	}	friend std::ostream & operator<<(std::ostream &os, const Engine &e);	friend std::istream & operator>>(std::istream &is, const Engine &e);};std::ostream & operator<<(std::ostream &os, const Engine &e){	return os << e.windowWidth << " " << e.windowHeight;}std::istream & operator>>(std::istream &is, Engine &e){	return is >> e.windowWidth >> e.windowHeight;}BOOST_CLASS_VERSION(Engine, 1)void SaveEngine(const Engine &engine){	// create and open a character archive for output	std::ofstream ofs("filename.txt");	boost::archive::text_oarchive oa(ofs);    // write class instance to archive	oa.save(engine);	// Close the file	ofs.close();}void LoadEngine(Engine &engine){	// create and open a character archive for output	std::ifstream ifs("filename.txt");	boost::archive::text_iarchive ia(ifs);    // write class instance to archive	ia.load(engine);	// Close the file	ifs.close();}int main(int argc, char* argv[]){	Engine myEngine;	//myEngine.windowWidth = 800;	//myEngine.windowHeight = 600;	//SaveEngine(myEngine);	//LoadEngine(myEngine);	std::cout << myEngine.windowWidth << " " << myEngine.windowHeight << std::endl;	return 0;}
Baraclese
Baraclese
Quote:
Original post by Drew_Benton
Now my question is, how the heck would I have known to add const to those functions as well as order the header files that way?


It should be in the docs, I dont know if the issue about include order is in it though. And ur prog should not crash, I think you may be linking to the wrong serialization library or rtti/exception handling is not enabled in ur build. No cludgy workarounds like the ones that you introduced should be needed. In fact boost::serialization is so beautiful because its so simple.

Quote:
I think I'm just going to use my own stuff. I'm worried about stuff like this thoughout the rest of the boost docs and this all seems to be more of a hassal than a solution.


I agree that using some boost libraries requires reading the docs and in case of problems also the boost users mailing list.
Drew_Benton
Drew_Benton
By any chance, would anyone have a complete working example of properly using this library? Also, I followed the Boost's guide to how to build the libs, so if there is more that can be done than explained on that page, could someone point out that link as well? Thanks
Baraclese
Baraclese
I just compiled ur example and it runs fine (with mingw-gcc). I linked to the static serialization library (libboost_serialization-mgw-s.lib). This must be a mistake in your build settings. Maybe http://www.boost.org/libs/serialization/doc/implementation.html#vc71 is of help?
Drew_Benton
Drew_Benton
Quote:
Original post by Baraclese
I just compiled ur example and it runs fine (with mingw-gcc). I linked to the static serialization library (libboost_serialization-mgw-s.lib). This must be a mistake in your build settings. Maybe http://www.boost.org/libs/serialization/doc/implementation.html#vc71 is of help?


Oh sorry, I meant if there was an example of how to use this lib correctly without all of what I did. My last code paste does work, but it seems to defeat the purpose of this library hehe [wink] I will look into that page though, thanks
Baraclese
Baraclese
Well I used the first version of your code with my two fixes applied. It should work.
Drew_Benton
Drew_Benton
Quote:
Original post by Baraclese
Well I used the first version of your code with my two fixes applied. It should work.


Ah, ok. I ran it again after the changes and it crashed, but I made the project use RTTI and it works perfect now! I guess I will end up sticking with Boost for the time being, thanks to you [smile] r++

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.