so im new to templates and i dont understand the errors im gettin in my code.

the errors start where it says "sort double array" in the main and the errors are the same for all 6 function calls after that "cannot convert parameter 1 from double[10] to int []"

#include <iostream>
#include <ctime>

using namespace std;

//void initArray (int array[], int size);
void printArray (int array[],int size);
void insertionSort (int list[],int  size);

void main () {
	// Locals
	const int SIZE = 10;
	int array [SIZE] = { 5, 4, 8, 2, 3, 9, 0, 1, 7, 6};
	double dary [SIZE] = { 4.6, 2e-3, .04, -6e2, -4, 2e5, 9.002, -3e-2, .05, -1.5};
	char cary [SIZE] = { 'G', 'B', 'Z', 'V', 'P', 'A', 'E', 'J', 'M', 'B'};

	// Implement
	srand(time(NULL));
	
	// Sort Integer Array
	//initArray(array, SIZE);
	printArray(array, SIZE);
	insertionSort(array, SIZE);
	printArray(array, SIZE);

    // Sort Double Array
	printArray(dary, SIZE);
	insertionSort(dary, SIZE);
	printArray(dary, SIZE);

	// Sort Character Array
	printArray(cary, SIZE);
	insertionSort(cary, SIZE);
	printArray(cary, SIZE);

}

void initArray (int array[], int size) {
	for (int i = 0; i < size; i++)
		array[i] = rand()%size;
}

template <class TYPE>
void printArray (TYPE array[], TYPE size) {
	cout << "{ ";
	for (int i = 0; i < size; i++)
		cout << array[i] << ", ";
	cout << "\b\b}" << endl;
}

template <class TYPE>
void insertionSort (TYPE list[], TYPE size) {
	void insert (int list[], int current);
	for (int current = 1; current <= size-1; current++)
		insert(list, current);
}

template <class TYPE>
void insert (TYPE list[], TYPE current) {
	int temp = list[current];

	for (int walker = current-1; walker >= 0; walker--) {
		if (temp < list[walker])
			list[walker+1] = list[walker];
		else
			break;
	} // for loop
	list[walker+1] = temp;
}

Your function definition says that 'size' is of the same type as the data pointed at by the first parameter. You meant to use int, not TYPE, in declaring the type of the size argument.

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