-
template woes
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 ***
-
t in your case is an array, so the errors make sense. Try something like -
template <typename t>
t max5(t ar[5]);
-
replacing &ar with ar[5]
I get a whole new set of errors replacing &t with ar[5]
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 argu
ments of type 'int[5]' in function main()
Error E2094 temp.cpp 20: 'operator<<' not implemented in type 'ostream' for argu
ments of type 'double[5]' in function main()
*** 2 errors in Compile ***
I attmpted to replace it in the prototype alone then in both the prototype and function definiton. t resolves to an array of type (int or double) but i want the return type to be either int or double depending on the array. How do I implemnt this in my template?
-
>I get a whole new set of errors replacing &t with ar[5]
Well you would do. Was this a typo?
This works (but not tested on Borland) -
Code:
#include <iostream>
using namespace std;
// template , prototypes
template <typename t>
t max5(t ar[5]);
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[5])
{
t max_=0;
for (int i =0;i<4;i++)
{
if (ar[i]>max_)
max_=ar[i];
}
return max_;
}
-
here are the errors i get using the code
Code:
#include <iostream>
using namespace std;
// template , prototypes
template <typename t>
t max5(t ar[5]);
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[5])
{
t max_=0;
for (int i =0;i<4;i++)
{
if (ar[i]>max_)
max_=ar[i];
}
return max_;
}
C:\Borland\BCC55\src>bcc32 temp.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
temp.cpp:
Error E2040 temp.cpp 28: Declaration terminated incorrectly in function max5<int
>(int *)
Error E2451 temp.cpp 28: Undefined symbol 'i' in function max5<int>(int *)
Warning W8057 temp.cpp 34: Parameter 'ar' is never used in function max5<int>(in
t *)
Error E2040 temp.cpp 28: Declaration terminated incorrectly in function max5<dou
ble>(double *)
Error E2451 temp.cpp 28: Undefined symbol 'i' in function max5<double>(double *)
Warning W8057 temp.cpp 34: Parameter 'ar' is never used in function max5<double>
(double *)
*** 4 errors in Compile ***
-
t max5(t ar[5])
Here you are only passing one value to the function. Instead, try t ar[].
-
I belive the problem lies with the function type not with the passing by refrence t &a. t should resolve to int or double array, but the return type may not resolve to an int or double value. Logicly how could it resolve to both, for instance an int for the return type and int array for the parameter. This is my first attempt using a template. If there is a way to create a generic return type let me know.
Here is code with function return type set to double and the logic error (to find the max element of an array) fixed. It works though I wanted a generic return type to be set by the template.
Code:
// template program prblm 5 pg 370
#include <iostream>
using namespace std;
// template , prototypes
template <typename t>
double 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;
cout <<"Greatst element of double array="<<max5(numd)<<endl;
return 0;
}
template <typename t>
double max5(t &ar)
{
double max=0;
for (int i=0;i<5;i++)
{
if (max<ar[i])
{max=ar[i];}
}
return max;
}