Beginner TEMPlate help

I am trying to pass in the same array with different data types using template, I do not see the issue Please help

1
2
3
4
5
6
7
8
9
10
  int main() {
	int arr[5] = { 4, 1, 13, 3, 2 };
	double arr[5] = { 1.1, 4.1, 8.1, 5.2, 2.3 };
	string arr[5] = { "the", "student", "is", "in", "class" };
	
	cout<<maxFunction<double>(arr)<<endl;//getting an error does //not match argument list
	cout<<maxFunction<int>(arr)<<endl;
	
	system("pause");
	return 0;
Last edited on
How is this maxFunction declared ?

Also you cannot have more than one variable with the same name.
1
2
3
template<class T>
T maxFunction(T arr[])
{....}
This works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

template<class T>
T maxFunction(T arr[])
{
  return arr[0];
}

int main() 
{
  int IntArr[5] = { 4, 1, 13, 3, 2 };
  double DoubleArr[5] = { 1.1, 4.1, 8.1, 5.2, 2.3 };
  string StringArr[5] = { "the", "student", "is", "in", "class" };

  cout << maxFunction<double>(DoubleArr) << endl; 
  cout << maxFunction<int>(IntArr) << endl;
  cout << maxFunction<string>(StringArr) << endl;

  system("pause");
  return 0;
}
Thanks,

Can i pass a string into the same function in hopes to alphabetize it or should i make a separate function for that
Last edited on
You need a seperate function for that. maxFunction accepts only arrays.
Topic archived. No new replies allowed.