Need some help with this template example
on the prototype to the function I used the generic type from the template declaration t. Is this valid. C++ Primer Plus only used void as the return type, but as you see i want the return type to be either double or integer depending on what is sent.
Code:
// template program prblm 5 pg 370
#include <iostream>
using namespace std;
// template , prototypes
template <typename t>
t max5(t &ar);
int main()
{
int numi[5]={10,4,5,9,8};
double numd[5]={10.1,30.2,15.34,70.1,80};
for (int i=0; i<5;i++)
{
cout << "Integer array element #"<< i<<" ="<< numi[i] << ".\t";
cout << "Double array element #"<< i<<" ="<< numd[i] << ".\n";
}
cout <<"Greatst element of integer array="<<max5(numi)<<endl;// error here
cout <<"Greatst element of double array="<<max5(numd)<<endl;
return 0;
}
template <typename t>
t max5(t &ar)
{
t max;
for (int=0;i<4;i++)
{
if (ar[i]>ar[i+1])
max=ar[i];
}
return max;
}
here are the errors
C:\Borland\BCC55\src>bcc32 temp.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
temp.cpp:
Error E2094 temp.cpp 19: 'operator<<' not implemented in type 'ostream' for arguments of type 'int[5]' in function main()
Error E2094 temp.cpp 20: 'operator<<' not implemented in type 'ostream' for arguments of type 'double[5]' in function main()
*** 2 errors in Compile ***