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 missed const 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

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -