I am new to templates in C++ and i attempted to make one with two numeric values:
template <
typename T, typename T2,
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type,
typename = typename std::enable_if<std::is_arithmetic<T2>::value, T2>::type>
T distance(vertex<T2>& p_Vertex1, vertex<T2>& p_Vertex2) {
return sqrt(pow((p_Vertex2.x - p_Vertex1.x), 2) +
pow((p_Vertex2.y - p_Vertex1.y), 2));
}
Errors:
1> C: \ Users \ User \ Desktop \ Library \ src \ vertex.cpp (4): warning C4348: 'distance': redefinition of the default parameter. Parameter 3.
1> C: \ Users \ User \ Desktop \ Library \ include \ Utils \ vertex.h (30): message: see the declaration of 'distance'
1> C: \ Users \ User \ Desktop \ Library \ src \ vertex.cpp (4): warning C4348: 'distance': redefinition of the default parameter. Parameter 4.
1> C: \ Users \ User \ Desktop \ Library \ include \ Utils \ vertex.h (30): message: see the declaration of 'distance'
this function is declared in vertex.h and defined in vertex.cpp; vertex is a template class that accepts a numeric value.
How am i using it:
vertex<double> v1(9.34342342, 5.34);
vertex<double> v2(69.67, 5.45);
float dist = distance<float, double>(v1, v2);
std::cout << "Distance: " << dist << std::endl;
Solved:
I solved moving the template function the same header file.