shootah
asked on
Boost Archive intialization as a class member
I am currently serializing data in a class in which a member function is called and passed the data and I declare a static boost::archive in that function and serialize the data to a binary file. This works fine when I deserialize (in a while loop with EOF condition) but now I would like to serialize to the same file/archive from other functions in the class. The Boost tutorials do not show any other way to intialize an archive.
Basically I want to declare a boost::archive::binary_oar chive as a member variable. Problem is, how do I initialize it, say, in the constructor after I have opened a file stream? My instinct tells me this is not allowed, but I was hoping there was a way to do this.
(FYI, I've tried to serialize by declaring different instances of the archive file in each function with a common file stream, but on deserialization, i get an "invalid signature" exception).
Basically I want to declare a boost::archive::binary_oar
(FYI, I've tried to serialize by declaring different instances of the archive file in each function with a common file stream, but on deserialization, i get an "invalid signature" exception).
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>"ar<<data" doesn't compile
Well, that should simply be '(*m_ar)<<data;' - since you are using a pointer, you need to dereference it.
Well, that should simply be '(*m_ar)<<data;' - since you are using a pointer, you need to dereference it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I love experts exchange. It magnifies my inept programming knowledge every time. :)
jkr,
I implemented evilrix's suggestion with your help and it works. I tried your second implementation as well but there seems to be an exception thrown. I'm not sure what the exception is because it falls into the catch all. It does not get caught by (boost::archive::archive_e xception) so maybe it's a stream exception or something. Creating the archive off the stack is preferable (seems cleaner I guess). Is there a way to discover the exception thrown? Maybe via gdb?
jkr,
I implemented evilrix's suggestion with your help and it works. I tried your second implementation as well but there seems to be an exception thrown. I'm not sure what the exception is because it falls into the catch all. It does not get caught by (boost::archive::archive_e
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> It does not get caught by (boost::archive::archive_e xception)
All boost exceptions derive from std::exception so just catch that by reference and use the what() member to display the reason.
http://www.cplusplus.com/reference/std/exception/exception/
All boost exceptions derive from std::exception so just catch that by reference and use the what() member to display the reason.
http://www.cplusplus.com/reference/std/exception/exception/
catch(std::exception const & e)
{
std::cerr << e.what();
}
ASKER
Yep that worked. Thanks.
ASKER
Last comment was in reference to jkr's suggestion btw.
ASKER
evilrix, thanks, i'll keep that in mind in the future.
BTW: AFAIK you can't create an archive object with a stream what is not open (at least, not with my somewhat limited experience of this part of the boost library).
ASKER
Yeah, that's what it looks like. Once the stream was initialized per jkr's suggestion it seemed to work.
I was guessing that the binary_oarchive might check the stream state, thus the 2nd suggestion - yet I have to admit it was just a shot in the dark ;o)
Hahaha... I've been working with archive just recently so I should have realised earlier what the problem was. It only dawned on me after I posted the comment about how to catch the exception. Heh -- it's been a long day at the office :)
>>it's been a long day at the office :)
Still there and trying to ignore the urge to punch the developers of MS' 'MIDL' in the stomach ;o)
Still there and trying to ignore the urge to punch the developers of MS' 'MIDL' in the stomach ;o)
>> trying to ignore the urge to punch the developers of MS' 'MIDL'
Don't fight it... seriously -- they deserve it :)
Don't fight it... seriously -- they deserve it :)
Indeed - especially when a line like
int BeapInit([in, string] const char* pszInit);
results in generated code that reads
/* interface Beap */
/* [auto_handle][version][uui d] */
int BeapInit(
/* [string][in] */ const unsigned char *pszInit);
and you are looking for the reason of the 'unresolved external' errors for over an hour pulling your hair out :-(
int BeapInit([in, string] const char* pszInit);
results in generated code that reads
/* interface Beap */
/* [auto_handle][version][uui
int BeapInit(
/* [string][in] */ const unsigned char *pszInit);
and you are looking for the reason of the 'unresolved external' errors for over an hour pulling your hair out :-(
That'll teach you to use COM :)
So glad I am not a Windows programmer!!!
So glad I am not a Windows programmer!!!
COM? Que? RPC! ;o)
ASKER
"no match for ‘operator<<’ in ‘((CDmsoHlaFederateAmbassa
If I'm not getting what you are asking me to do, maybe you can post an example? Here is what I've implemented more or less.
Open in new window