Hello,
I have a problem creating a template class with copy ctor and cast operator to class of base template (using gcc 4.2). The program compiles if only one of them is defined but is not compiling if both (copy ctor and cast operator) are defined.
The compiler error is
error: no matching function for call to 'Vector<float>::Vector(Vector<float>)'
note: candidates are: Vector<F>::Vector(Vector<F>&) [with F = float]
and is referring to the assignment:
Vector<float> pf = pd;
Here is simple example of the class:
vector.h:
template<class F>
class Vector
{
public:
Vector(F x1, F y1, F z1):x(x1),y(y1),z(z1)
{
}
// This copy constructor is ok if cast operator is not defined
Vector(Vector<F>& source)
{
}
// This cast operator is ok if copy constructor is not defined
template<class D> operator Vector<D> () const;
F x,y,z;
};
vector.cpp
template <class F> template<class D>
Vector<F>::operator Vector<D> () const
{
return Vector<D>(x,y,z);
}
main.cpp
void TestVector(Vector<double> v)
{
}
int main()
{
Vector<double> pd(5.6,3.4,2.4);
// use the cast operator works if copy ctor is not defined
Vector<float> pf = pd;
// use the copy ctor works if cast operator is not defined
TestVector(pd);
}