Function templates and passing arrays as arguments

Hey, I've been writing this program that is suppose to sort an array of elements. The elements could be of type int, double, or char. So, instead of writing three separate functions for each data type, I am suppose to use template functions. There are two functions. sort and swap. When sort is called the number of elements and the array that holds the actual values are used as arguments. Inside sort, a loop begins at the beginning of the array of elements. A nested loop then tests two elements at a time. If the second value is less than the first value then, the swap function is then called. swap is suppose to call the two elements of the array as arguments. THIS IS WHERE I RUN INTO PROBLEMS. As you can see in the code that I bolded. swap's parameters are (T a[], T a[]) I did this because when swap is eventually called, it will be using the same array as an argument, just with different indexes. Now, when I build the solution, I get the following errors from the output:
error C2086: 'T a[]' : redefinition
see declaration of 'a'

I get this error for both the prototype of swap and when I write the body of swap. I wonder if I have to put something inside the brackets to represent the index? But, when I try to put a variable, the compiler complains that the variable is unidentified. If you notice, the sort function has one parameter that is similar. But, I have gotten no complaints from the compiler on that. This is what leads me to believe that I need to diversify the parameters in swap. I just do not know how to go about doing this. If anyone has any suggestions, they would be greatly appreciated. Thank you for your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
using namespace std;

//Template class
template <class T>
//sort prototype
void sort (int n, T a[]);

//Template class
template <class T>
//swap prototype
void swap (T a[], T a[]);


void main()
{
	
	//declare variable n as int. Will store # of elements to be read.
	int n;
	//declare variable whichType. Will determine the type of array to be passed to sort function
	int whichType;
	//declare 3 arrays. One int, One double, and One char
	int a[25];
	double b[25];
	char c[25];
	//ask User what type of data they want to use for the array
	cout << "Which type do you want for your array of numbers?\n";
	cin >> whichType;
	//Three different if statements will be used: One for each data type
	//Each if statement will include:
	//Entering a value for n
	//Entering values for the array
	//print out the list in original order - This might take place in the sort function
	//sort function is called: arguments are int n and T a[]
	if(whichType == 1)
	{	
		cout << "Enter the number of elements.\n";
		cin >> n;
		cout << "populate the elements.\n";
		for(int i = 0; i < n; i++)
			cin >> a[i];
		for(int i = 0; i < n; i++)
			cout << a[i] << " ";
		sort (n, a);
	}

	if(whichType == 2)
	{	
		cout << "Enter the number of elements.\n";
		cin >> n;
		cout << "populate the elements.\n";
		for(int i = 0; i < n; i++)
			cin >> b[i];
		for(int i = 0; i < n; i++)
			cout << b[i] << " ";
		sort (n, b);
	}

	if(whichType == 3)
	{	
		cout << "Enter the number of elements.\n";
		cin >> n;
		cout << "populate the elements.\n";
		for(int i = 0; i < n; i++)
			cin >> c[i];
		for(int i = 0; i < n; i++)
			cout << c[i] << " ";
		sort (n, c);
	}
	//numbers should be in order after the sort function
	//cout this sorted list
		
}//main



//Template class
template <class T>
//sort function
void sort (int n, T a[])
{
	//declare variables last and m as int
	int last;
	int m;
	//May print out original order of arrays here. Not sure
	//Will use for loop and nested for loop
	//The first for loop will increment up to the # of elements that are in the array
	for(last = 1; last < n; ++last)
	//The second loop will run so eventually all numbers are tested and sorted properly
		for(m = last; m > 0; --m)
		{	//Inside this loop there is a specific if statement that tests two values
			//If the value to the right is less than the value to the left, swap them
			if(a[m] < a [m-1]) 
				//The swap function is then called with arguments a[m] and a[m-1]
				{ swap(a[m], a[m-1]); }
			//If not, then break out of the loop
			else break;
		}
		cout << T a[];
}// sort

//Template class
template <class T>
//swap function
void swap (T a[], T a[])
{
	//Cout the input to swap 
	//declare variable temp as T
	T temp;

	temp = a[];  //Like previous project, temp will hold the value of a[m]
	a[] = a[];  //a[m] will take the value of a[m-1]
	a[] = temp;  //a[m-1] will take the value in temp, which was the old value of a[m]

	//Cout the output from swap.
}
Did you not expect to get an error when giving two parameters the same name? How did you not notice and not even think this would be problematic?
Thanks for your help :D
Topic archived. No new replies allowed.