I am trying to write a function template to receive an array of any type of data. The function should return the average of the first 3 elements of the array and the return value will be the same as the type of data in the array.
I am new to function templates. When I run my code I get an error message that states: subscript is not of integral type. This error message is in the total area of the function.
If anyone can help me figure out my error I would appreciate it.
Thanks,
R.
Here is my code:
Code:#include <iostream> using namespace std; template <class T> T Data(T array[]) { T average; T total = 0.0; T cnt; T num; for(cnt = 0; cnt < num; cnt++) total += array[cnt]; return average = total / 3; } int main() { int Array[] = {2, 3, 4, 5, 6, 7}; float Array2[] = {4.5, 6.7, 7.5, 9.5}; cout << Data(Array) << endl; cout << Data(Array2) << endl; system("pause"); return 0; }