New to this topic,and need some help here. I have a class which use the scoped_ptr as a member variable. Class CTest { public: CTest(); Initialize(); ~CTest(); private: scoped_ptr<CTest2> m_spTest2; } For some reason, that I cannot initialize m_spTest2 in the member initialization list of the constructor, so I have to initialize in some other functions like in Initialize. CTest::Initialize() { int aa, bb; m_spTest2=new CTest2(aa, bb); } The compiler will complain about that, so what's the right way to do it. Thanks, Jianwei
John Sun wrote: <snip> You can't assign anything to a scoped ptr, you have to use it's reset member function. (See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm.)
CTest::Initialize() { int aa, bb; m_spTest2=new CTest2(aa, bb);
m_spTest2.reset(new CTest2(aa, bb));
}
Markus
Markus , Appreciate your help, can you explain what's the rationale that you use "reset" instead of a simple more natural way of "assign". John Markus Schöpflin wrote:
John Sun wrote:
<snip>
You can't assign anything to a scoped ptr, you have to use it's reset member function. (See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm.)
CTest::Initialize() { int aa, bb; m_spTest2=new CTest2(aa, bb);
m_spTest2.reset(new CTest2(aa, bb));
}
Markus
On 30/09/05, John Sun
Appreciate your help, can you explain what's the rationale that you use "reset" instead of a simple more natural way of "assign".
For one, that's the way that auto_ptr does it. Second, plain assignment can cause hidden bugs because it would take ownership of something in the middle where it's hard to notice. You can just construct a new smart pointer and assign the temporary to the one you want to change, anyways. - Scott
I am new to Boost and am having no luck getting a nested vector construct to serialize. Pretty sure I have the rigth includes, but keep getting various errors out of g++ (sample below) Anyone have some sample code that has the right set of magic in it. Thanks - ;;peter = = = start of a sample of the errors I get = = = = = = g++ -I. -I /usr/kde/3.3/include -I /usr/qt/3/include -I /usr/include/boost/test `getconf LFS_CFLAGS` -g -O2 -c testVec.cc /usr/include/boost/archive/detail/oserializer.hpp: In function `void boost::archive::save(Archive&, T&) [with Archive = boost::archive::text_oarchive, T = stashable_t]': /usr/include/boost/archive/basic_text_oarchive.hpp:78: instantiated from `voidboost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = stashable_t, 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 = stashable_t, Archive = boost::archive::text_oarchive]' genDigest.cc:209: instantiated from here /usr/include/boost/archive/detail/oserializer.hpp:566: error: incomplete type `boost::STATIC_ASSERTION_FAILURE< false>' used in nested name specifier /usr/include/boost/archive/detail/oserializer.hpp:566: error: size of array has non-integral type `<type error>' = = = end of sample of error - some are much longer ;-) = = =
At the place where the error is encountered you will find the following comment: // if your program traps here, it indicates taht your doing one of the following: // a) serializing an object of a type marked "track_never" through a pointer. // b) saving an non-const object of a type not markd "track_never) // Either of these conditions may be an indicator of an error usage of the // serialization library and should be double checked. See documentation on // object tracking. Robert Ramey Peter Broadwell wrote:
I am new to Boost and am having no luck getting a nested vector construct to serialize. Pretty sure I have the rigth includes, but keep getting various errors out of g++ (sample below)
Anyone have some sample code that has the right set of magic in it.
Thanks -
;;peter
= = = start of a sample of the errors I get = = = = = =
g++ -I. -I /usr/kde/3.3/include -I /usr/qt/3/include -I /usr/include/boost/test `getconf LFS_CFLAGS` -g -O2 -c testVec.cc /usr/include/boost/archive/detail/oserializer.hpp: In function `void boost::archive::save(Archive&, T&) [with Archive = boost::archive::text_oarchive, T = stashable_t]': /usr/include/boost/archive/basic_text_oarchive.hpp:78: instantiated from `voidboost::archive::basic_text_oarchive<Archive>::save_override(T&, int) [with T = stashable_t, 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 = stashable_t, Archive = boost::archive::text_oarchive]' genDigest.cc:209: instantiated from here /usr/include/boost/archive/detail/oserializer.hpp:566: error: incomplete type `boost::STATIC_ASSERTION_FAILURE< false>' used in nested name specifier /usr/include/boost/archive/detail/oserializer.hpp:566: error: size of array has non-integral type `<type error>'
= = = end of sample of error - some are much longer ;-) = = =
I initialize scoped_ptr in the member initialization list frequently. You should be able to do it as well. However, I can't guess at what your problem is because 1) I don't know what error you are getting 2) I don't know what the constructor for CTest2 looks like What I suspect is that you are passing the wrong argument to the initializer for m_spTest2. It should be something like : m_spTest2(new CTest2(aa, bb)) Where you'd get instances for aa and bb from the default CTest construct is unclear, but that could the actual problem. Alternately, you would need to have CTest2 defined "near" the definition of the CTest() constructor, because the CTest constructor invokes a method of CTest2. If defined outside the class, that would be somewhere before the definition. If defined in CTest, then it could be as late as the closing brace of the class (if CTest2 is a nested class). At 06:04 PM 9/29/2005, you wrote:
New to this topic,and need some help here.
I have a class which use the scoped_ptr as a member variable.
Class CTest { public: CTest(); Initialize(); ~CTest(); private: scoped_ptr<CTest2> m_spTest2; }
For some reason, that I cannot initialize m_spTest2 in the member initialization list of the constructor, so I have to initialize in some other functions like in Initialize.
CTest::Initialize() { int aa, bb; m_spTest2=new CTest2(aa, bb); }
The compiler will complain about that, so what's the right way to do it.
Thanks, Jianwei
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Alan M. Carroll wrote:
I initialize scoped_ptr in the member initialization list frequently. You should be able to do it as well. However, I can't guess at what your problem is because
1) I don't know what error you are getting 2) I don't know what the constructor for CTest2 looks like
What I suspect is that you are passing the wrong argument to the initializer for m_spTest2. It should be something like : m_spTest2(new CTest2(aa, bb))
Where you'd get instances for aa and bb from the default CTest construct is unclear, but that could the actual problem.
[Yes, that's exactly where my problem comes from, because I cannot get the aa, bb from the CTest constructor, and it was passed in later on by the user of the class.]
Alternately, you would need to have CTest2 defined "near" the definition of the CTest() constructor, because the CTest constructor invokes a method of CTest2. If defined outside the class, that would be somewhere before the definition. If defined in CTest, then it could be as late as the closing brace of the class (if CTest2 is a nested class).
At 06:04 PM 9/29/2005, you wrote:
New to this topic,and need some help here.
I have a class which use the scoped_ptr as a member variable.
Class CTest { public: CTest(); Initialize(); ~CTest(); private: scoped_ptr<CTest2> m_spTest2; }
For some reason, that I cannot initialize m_spTest2 in the member initialization list of the constructor, so I have to initialize in some other functions like in Initialize.
CTest::Initialize() { int aa, bb; m_spTest2=new CTest2(aa, bb); }
The compiler will complain about that, so what's the right way to do it.
Thanks, Jianwei
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Then your possible solutions are: 1) Require aa,bb to be passed in to the CTest constructor. 2) Add a default constructor for CTest2 and a method to set the aa,bb at some later point. 3) Use scoped_ptr<T>::reset to put a CTest2 instance in the member when you get a valid aa,bb (as described by other responders). Personally, I'd go with (1) or (2) because otherwise you have to always check whether m_spTest2 is set or not. I like to have my scoped_ptrs always pointing at something, or it's a error condition that needs to be handled. In particular, (2) means that the knowledge of what to do if the CTest2 isn't valid is deferred to CTest2, rather than being in CTest (i.e. less coupling). That's style, though, not an actual requirement of the class. At 08:19 AM 10/1/2005, you wrote:
Alan M. Carroll wrote:
I initialize scoped_ptr in the member initialization list frequently. You should be able to do it as well. However, I can't guess at what your problem is because
1) I don't know what error you are getting 2) I don't know what the constructor for CTest2 looks like
What I suspect is that you are passing the wrong argument to the initializer for m_spTest2. It should be something like : m_spTest2(new CTest2(aa, bb))
Where you'd get instances for aa and bb from the default CTest construct is unclear, but that could the actual problem.
[Yes, that's exactly where my problem comes from, because I cannot get the aa, bb from the CTest constructor, and it was passed in later on by the user of the class.]
participants (6)
-
Alan M. Carroll
-
John Sun
-
Markus Schöpflin
-
me22
-
Peter Broadwell
-
Robert Ramey