Why am I getting this errors when running this program where i tried to create 2 specialized templates to use int and double:
#include <iostream>
using namespace std;
template<>
int sum<int>(int* array, int start, int stop, int init = int())
{
int* stp = (array + stop);
int* str = (array + start);
while(str != stp)
{
init += *str++;
}
return init;
}
template<>
double sum<double>(double* array, double start, double stop, double init = double())
{
double* stp = (array + stop);
double* str = (array + start);
while(str != stp)
{
init += *str++;
}
return init;
}
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9};
cout << sum<>(a, 2, 9);
}
Errors:
/5_Templates_in_Depth/6/main.cc:12: error: expected initializer before ‘<’ token
../5_Templates_in_Depth/6/main.cc: In function ‘int main()’:
../5_Templates_in_Depth/6/main.cc:40: error: ‘sum’ was not declared in this scope
../5_Templates_in_Depth/6/main.cc:40: error: expected primary-expression before ‘>’ token
../5_Templates_in_Depth/6/main.cc:40: warning: left-hand operand of comma has no effect
../5_Templates_in_Depth/6/main.cc:40: warning: right-hand operand of comma has no effect