How to write a copy constructor for Template Class - C++ -
How to write a copy constructor for Template Class - C++ -
in header file have
template <typename t> class vector { public: // constructor , other things const vector& operator=(const vector &rhs); };
and here 1 declaration i've tried far
template <typename t> vector& vector< t >::operator=( const vector &rhs ) { if( != &rhs ) { delete [ ] array; thesize = rhs.size(); thecapacity = rhs.capacity(); array = new t[ capacity() ]; for( int = 0; < size(); i++ ){ array[ ] = rhs.array[ ]; } } homecoming *this; }
this compiler telling me
in file included vector.h:96, main.cpp:2: vector.cpp:18: error: expected constructor, destructor, or type conversion before ‘&’ token make: *** [project1] error 1
how declare re-create constructor?
note: project , cannot alter header declaration, suggestions this, while useful, not helpful in particular instance.
thanks help!
note: declare assignment operator, not re-create constructor
you missedconst
qualifier before homecoming type you missed template argument(<t>
) homecoming type , function argument use this:
template <typename t> const vector<t>& vector<t>::operator=(const vector<t>& rhs)
c++ templates copy-constructor
Comments
Post a Comment