errors serializing pointers on Fedora Core 5
The following tiny program generates two compiler errors on FC 5
(which comes with g++ 4.1.1 and boost 1.33.1):
#include <iostream>
#include <fstream>
#include <string>
#include òù has no member named òøserializeòù Error No. 1 can be fixed by making pFoo a const pointer type, i.e., by
changing the declaration/assignment statement to be
string *const pFoo = new string("foo");
I cannot figure out why such a change is necessary, as the
serialization library documentation does not indicate that it is
necessary to make pointers const in order to serialize them (as is
borne out by the demo.cpp code). Also, I cannot figure out how to fix
Error No. 2.
Any help would be much appreciated!
Thanks,
Dan.
Dan Bikel wrote:
The following tiny program generates two compiler errors on FC 5 (which comes with g++ 4.1.1 and boost 1.33.1):
#include <iostream> #include <fstream> #include <string> #include
#include #include using namespace std; using namespace boost;
int main(int argc, char **argv) { string * pFoo = new string("foo"); ofstream ofs("strfile"); { archive::text_oarchive oa(ofs); oa << pFoo; } }
Error No. 1: /usr/include/boost/archive/detail/oserializer.hpp:567: error: invalid application of -1òøsizeofòù to incomplete type òøboost::STATIC_ASSERTION_FAILURE<false>
Error No. 2: /usr/include/boost/serialization/access.hpp:109: error: -1òøstruct std::basic_string
òù has no member named òøserializeòù
Error No. 1 can be fixed by making pFoo a const pointer type, i.e., by changing the declaration/assignment statement to be string *const pFoo = new string("foo");
I cannot figure out why such a change is necessary, as the serialization library documentation does not indicate that it is necessary to make pointers const in order to serialize them (as is borne out by the demo.cpp code). Also, I cannot figure out how to fix Error No. 2.
Any help would be much appreciated!
Thanks, Dan.
Dan Bikel wrote:
The following tiny program generates two compiler errors on FC 5 (which comes with g++ 4.1.1 and boost 1.33.1):
#include <iostream> #include <fstream> #include <string> #include
#include #include using namespace std; using namespace boost;
int main(int argc, char **argv) { string * pFoo = new string("foo"); ofstream ofs("strfile"); { archive::text_oarchive oa(ofs); oa << pFoo; } }
Error No. 1: /usr/include/boost/archive/detail/oserializer.hpp:567: error: invalid application of -1òøsizeofòù to incomplete type òøboost::STATIC_ASSERTION_FAILURE<false>
Error No. 2: /usr/include/boost/serialization/access.hpp:109: error: -1òøstruct std::basic_string
òù has no member named òøserializeòù
Error No. 1 can be fixed by making pFoo a const pointer type, i.e., by changing the declaration/assignment statement to be string *const pFoo = new string("foo");
I cannot figure out why such a change is necessary, as the serialization library documentation does not indicate that it is necessary to make pointers const in order to serialize them (as is borne out by the demo.cpp code).
This is explained in the "Rationale" section of the documentation.
Also, I cannot figure out how to fix Error No. 2.
Any help would be much appreciated!
The following program compiles. The problem is that std::string
is marked with a "primitive" serialization trait. It's the only
non built-in type so marked. Pointers to primitive types
cannot be serialized. The standard advice is to use
BOOST_STRONG_TYPEDEF to make a divferently
named version of the same type. This doesn't work
for string - so I made the same thing in this example.
There are other ways to do this - e.g. casting - but this
should give you the idea
#include <iostream>
#include <fstream>
#include
Thanks, Dan.
Robert Ramey wrote:
Error No. 1 can be fixed by making pFoo a const pointer type, i.e., by changing the declaration/assignment statement to be string *const pFoo = new string("foo");
I cannot figure out why such a change is necessary, as the serialization library documentation does not indicate that it is necessary to make pointers const in order to serialize them (as is borne out by the demo.cpp code).
This is explained in the "Rationale" section of the documentation.
Okay, I read through the (long) scenario described in the Rationale section, and I guess I agree with this design decision, but in the main documentation--the Tutorial section in particular--there should not be code snippets that are incorrect. I'm thinking of the example in the "Pointers" section of the tutorial, where the sample code shows *non-const* bus_stop pointers being serialized in order to serialize "bus_route" objects. At the very least, you should have correct code and a hyperlink to the Rationale section explaining why the const is there. Anyway, I don't mean this to sound harsh, because I'm very grateful for your prompt answer to my question! --Dan.
Dan Bikel wrote:
Robert Ramey wrote:
Error No. 1 can be fixed by making pFoo a const pointer type, i.e., by changing the declaration/assignment statement to be string *const pFoo = new string("foo");
I cannot figure out why such a change is necessary, as the serialization library documentation does not indicate that it is necessary to make pointers const in order to serialize them (as is borne out by the demo.cpp code).
This is explained in the "Rationale" section of the documentation.
Okay, I read through the (long) scenario described in the Rationale section, and I guess I agree with this design decision, but in the main documentation--the Tutorial section in particular--there should not be code snippets that are incorrect. I'm thinking of the example in the "Pointers" section of the tutorial, where the sample code shows *non-const* bus_stop pointers being serialized in order to serialize "bus_route" objects. At the very least, you should have correct code and a hyperlink to the Rationale section explaining why the const is there.
That section of the manual uses the "&" operator rather than the << operator. This operator doesn't enforce the "trap" - in part to give an escape for those who find the trap unhelpful. Actually, there should be a trap - leading to a better error message - for detecting the case you've got here - serialization of a pointer to a prmitive - which needs special treatment. But then - we really should implement concept checking to detect these errors and display better messages. - its on my to do list.
Anyway, I don't mean this to sound harsh, because I'm very grateful for your prompt answer to my question!
--Dan.
participants (2)
-
Dan Bikel
-
Robert Ramey