[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppSharedPtr
A smart pointer from the Boost C++ Library that automatically deletes the instance it is pointing to, when it goes out of scope.
The difference with std::auto_ptr is, that a copy of a shared pointer is symmetrical. Due to this, a shared_ptr can be put in a vector.
Internally, a shared_ptr keeps track how many copies of the pointer are made, only calling delete when the last one goes out of scope.
shared_ptr is not in the STL, but in the Boost C++ Library and TR1.
caused by:
The compile error is solved by changing this to:
I still do not understand why it occurs (as on top of the header file boost/assert.hpp is #included.
This error started to occur when I split a multi-unit project over multiple folders, in which I had defined by own assert.h and assert.cpp files. When I
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppSharedPtr
(C++) boost::shared_ptr
(same as tr1::shared_ptr)A smart pointer from the Boost C++ Library that automatically deletes the instance it is pointing to, when it goes out of scope.
int main() { const boost::shared_ptr<MyClass> myClass(new MyClass); return 0; }
- include <boost/shared_ptr.hpp>
The difference with std::auto_ptr is, that a copy of a shared pointer is symmetrical. Due to this, a shared_ptr can be put in a vector.
class MyClass { public: MyClass(const int& value) : mValue(value) { ; } void cout() const { std::cout << mValue << std::endl; } private: const int mValue; }; int main() { const int size = 10; std::vector< boost::shared_ptr<MyClass> > myVector(size); for (int i=0; i!=size; ++i) myVector[i].reset(new MyClass(i)); std::cout << "Before shuffling: " << std::endl; for (int i=0; i!=size; ++i) myVector[i]->cout(); std::random_shuffle(myVector.begin(),myVector.end()); std::cout << "After shuffling: " << std::endl; for (int i=0; i!=size; ++i) myVector[i]->cout(); }
- include <iostream>
- include <stdlib>
- include <vector>
- include <boost/shared_ptr.hpp>
Internally, a shared_ptr keeps track how many copies of the pointer are made, only calling delete when the last one goes out of scope.
shared_ptr is not in the STL, but in the Boost C++ Library and TR1.
Nasty compile error with boost::shared_ptr
[C++ Error] shared_ptr.hpp(124): E2034 Cannot convert 'Y *' to 'BirdBase *' [C++ Error] shared_count.hpp(82): E2450 Undefined structure 'sp_counted_impl_p<Y>' [C++ Error] shared_count.hpp(82): E2315 'sp_counted_impl_p<Y>()' is not a member of 'sp_counted_impl_p<Y>', because the type is not yet defined [C++ Error] shared_count.hpp(82): E2034 Cannot convert 'sp_counted_impl_p<Y> *' to 'sp_counted_base *' [C++ Error] checked_delete.hpp(32): E2034 Cannot convert 'Y' to 'bool'
caused by:
//Removes the boost::shared_ptr<BirdBase> mMale from the class Couple
boost::shared_ptr<BirdBase> Couple::getMale()
{
boost::shared_ptr<BirdBase> male(mMale);
mMale.reset(0);
return male;
}
The compile error is solved by changing this to:
//Removes the boost::shared_ptr<BirdBase> mMale from the class Couple
boost::shared_ptr<BirdBase> Couple::getMale()
{
boost::shared_ptr<BirdBase> male(mMale);
mMale.reset(); //Remove the 0
return male;
}
shared_ptr.hpp(253): Call to undefined function 'assert'
I don't know why, but I've got this error in the following section of code, in the second line.
T * operator-> () const // never throws
{
BOOST_ASSERT(px != 0);
return px;
}
I still do not understand why it occurs (as on top of the header file boost/assert.hpp is #included.
This error started to occur when I split a multi-unit project over multiple folders, in which I had defined by own assert.h and assert.cpp files. When I
- removed the Forms from 'Project | Options | Forms' from 'Auto-Create Forms' to 'Available', compiled successfully (!) and set the Forms back to their desired availability.
- removed all files from the project
- removed all project directories (i.e. the multiple paths for the project)
- renamed my assert files to ThorAssert.h and ThorAssert.cpp
- added the files to the project again (with the help of 'Unresolved external errors')
- #included the newly named assert files
[C++ Error] shared_ptr.hpp(162): E2034 Cannot convert 'Base * const' to 'Derived *'
The reason for this error is exactly as stated. The main problem is that the compiler does not take you to the line which causes it. It is caused by a line in which you try to use a base class for e.g. a function taking a derived class. An example is the code below (the full code is at inheritance).//--------------------------------------------------------------------------- struct Base { Base(const int& value) : mValueBase(value) {} const int mValueBase; }; //--------------------------------------------------------------------------- struct Derived : public Base { Derived(const int& value1, const int& value2) : Base(value1), mValueDerived(value2) {} const int mValueDerived; }; //--------------------------------------------------------------------------- void funcDerived(const boost::shared_ptr<Derived> & derived) { //Does nothing derived; } //--------------------------------------------------------------------------- int main() { boost::shared_ptr<Base> base(new Base(0)); funcDerived(base); //This line causes the compile error }
- include <boost/shared_ptr.hpp>
Code links
Links
- Boost C++ Library shared_ptr documentation: http://www.boost.org/libs/smart_ptr/shared_ptr.htm
- Boost C++ Library smart pointers programming techniques: http://www.boost.org/libs/smart_ptr/sp_techniques.html
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
