novice wrote:
Using STL I wanted to create a vector of pointers.For this purpose i
have defined a class thin_ptr as follows:
//thin_ptr
template <typename T ,const bool tAutoDestroy = false>
struct thin_ptr
{
typedef T wrapped_type;
T* p;
thin_ptr(T* p) {this->p = p;}
thin_ptr(const thin_ptr& x) {this->operator=(x);}
~thin_ptr() { if (tAutoDestroy) destroy(); }
int destroy() {delete p; p = 0; return 0;}
T* release() {T* tp = p; p = 0; return tp;}
T* operator -> () const { return p;}
T& operator * () const { return *p;}
T** operator & () const {return &p;}
operator T* () const { return p;}
T* operator = (T* p) {this->p = p;}
// for STL containers
bool operator < (const thin_ptr& x) const
{ return *p < *x.p;}
bool operator!() const {return p == 0;}
};
which is giving the error:
In member function `T** thin_ptr<T, tAutoDestroy>::operator&() const
[with T = TSPGene<RandomCRT>, bool tAutoDestroy = false]':
/usr/include/c++/3.2.2/bits/stl_uninitialized.h:87: instantiated from
`_ForwardIter std::__uninitialized_copy_aux(_InputIter, _InputIter,
_ForwardIter, __false_type) [with _InputIter =
__gnu_cxx::__normal_iterator<thin_ptr<TSPGene<RandomCRT>, false>*,
std::vector<thin_ptr<TSPGene<RandomCRT>, false>,
std::allocator<thin_ptr<TSPGene<RandomCRT>, false> > > >, _ForwardIter
= __gnu_cxx::__normal_iterator<thin_ptr<TSPGene<RandomCRT>, false>*,
std::vector<thin_ptr<TSPGene<RandomCRT>, false>,
std::allocator<thin_ptr<TSPGene<RandomCRT>, false> > > >]'
/usr/include/c++/3.2.2/bits/stl_uninitialized.h:112: instantiated
from `_ForwardIter std::uninitialized_copy(_InputIter, _InputIter,
_ForwardIter) [with _InputIter =
__gnu_cxx::__normal_iterator<thin_ptr<TSPGene<RandomCRT>, false>*,
std::vector<thin_ptr<TSPGene<RandomCRT>, false>,
std::allocator<thin_ptr<TSPGene<RandomCRT>, false> > > >, _ForwardIter
= __gnu_cxx::__normal_iterator<thin_ptr<TSPGene<RandomCRT>, false>*,
std::vector<thin_ptr<TSPGene<RandomCRT>, false>,
std::allocator<thin_ptr<TSPGene<RandomCRT>, false> > > >]'
/usr/include/c++/3.2.2/bits/stl_vector.h:903: instantiated from `void
std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =
thin_ptr<TSPGene<RandomCRT>, false>, _Alloc =
std::allocator<thin_ptr<TSPGene<RandomCRT>, false> >]'
/usr/include/c++/3.2.2/bits/stl_vector.h:498: instantiated from `void
std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp =
thin_ptr<TSPGene<RandomCRT>, false>, _Alloc =
std::allocator<thin_ptr<TSPGene<RandomCRT>, false> >]'
Main6.h:1194: instantiated from here
Main6.h:679: invalid conversion from `TSPGene<RandomCRT>* const*' to `
TSPGene<RandomCRT>**'
////////////////////////////////////////////////////////////////////////////////////////
I think the problem is that operator&() is a const method, which makes
the member variable thin_ptr<T>::p implicitly a const pointer (i.e.
T*const), so that &p is T*const *. You need a const_cast to make the
conversion from T* const * to T**. Alternatively, remove the const from
operator&().
The latter choice is your better bet.
_______________________________________________
help-gplusplus mailing list
help-gplusplus@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gplusplus