I seem to be very bad with playing around with pointers. Basically the specialization template is supposed to return the address of the longest of the 5 strings provided. I keep getting an error on line 20 about char vs char *. Please some help, a good explanation on the logic would be nice too.

//Exc_6.cpp - More template practice

#include <iostream>

using namespace std;

template <typename T>
T maxn(T array [], int n);

template <typename T>
T maxn(T * array[], int n);

int main()
{
	int intArray[6] = {1,2,3,4,5,6};
	double doubleArray[4] = {1.25, 2.5, 3.75, 5.01};
	char * str[5] = {"Test1", "Test 2", "Testing3", "Hi", "LOL"};
	int maxInt = maxn(intArray, 6);
	double maxDouble = maxn(doubleArray, 4);
	char * maxStr = maxn(str, 5);
	cout << "Max int value is: " << maxInt << endl;
	cout << "Max double value is: " << maxDouble << endl;
	cout << "Max string value located at: " << maxStr << endl;
	system("PAUSE");
}

template <typename T>
T maxn(T array [], int n)
{
	T max = array[0];
	for (int i = 0; i < n; i++)
	{
		if (array[i] > max)
			max = array[i];
	}
}

template <typename T>
T maxn(T * array[], int n)
{
	int max = array[0];
	for (int i = 0; i < n; i++)
	{
		if (array[i].size() > max.size())
			max = array[i];
	}
	return array[];
}

Your function call with

char * maxStr = maxn(str, 5);

Does it make sense to have

str.size();

since str is a c-string?

This article has been dead for over six months. Start a new discussion instead.