Reputation: 5125
In my header file I have
template <typename T>
class Vector {
public:
// constructor and other things
const Vector& operator=(const Vector &rhs);
};
and here is one declaration which I've tried so far
template <typename T> Vector& Vector< T >::operator=( const Vector &rhs )
{
if( this != &rhs )
{
delete [ ] array;
theSize = rhs.size();
theCapacity = rhs.capacity();
array = new T[ capacity() ];
for( int i = 0; i < size(); i++ ){
array[ i ] = rhs.array[ i ];
}
}
return *this;
}
this is what the compiler is telling me
In file included from Vector.h:96,
from main.cpp:2:
Vector.cpp:18: error: expected constructor, destructor, or type conversion before ‘&’ token
make: *** [project1] Error 1
How do I properly declare the copy constructor?
Note: This is for a project and I cannot change the header declaration, so suggestions like this, while useful, are not helpful in this particular instance.
Thanks for any help!
Upvotes: 0
Views: 4243
Reputation: 33661
NOTE: You declare an assignment operator, not copy constructor
const
qualifier before return type<T>
) for return type and function argumentUse this:
template <typename T>
const Vector<T>& Vector<T>::operator=(const Vector<T>& rhs)
Upvotes: 2