Hi everyone,
When I try to overload the operator+ for a template class, the gnu g++-4.x compiler generates the following error message on the 'return' statement in the body of the operator definition:
../Vector.h:73: error: no matching function for call to ‘Vector<double>::Vector(Vector<double>&)’
../Vector.h: In constructor ‘Vector<T>::Vector(const size_t&) [with T = double]’:
../Vector.h:69: instantiated from ‘Vector<T> Vector<T> :: operator+(const Vector<T>&) [with T = double]’
Voila the code:
Code:
template<typename T>
class Vector
{
public:
Vector( void);
explicit Vector( Vector<T> const&); // copy constructor
explicit Vector( size_t const&); // zero value
Vector<T> operator+( Vector<T> const& vec2)
{
Vector<T> result( vec2.size());
for( int i=0; i<result.size(); i++) {
result[i] = this->vec[i] + vec2[i];
}
return result; // line 73, which generates error message
}
private:
size_t N;
T* vec;
};
It searches for a kind of copy constructor; without the keyword 'explicit' it suggests the one-argument constructor as a candidate. But implementing this not-so-pretty copy constructor seems really awkward to me. Has it got something to do with all my 'const' declarations?
I know that I do not need to specialise the return type of the operator because of argument deduction, but my goal is to use traits to determine (map) the return type. This is a small simplification to hunt down the bug.
Thanks a lot in advance!!