Template Question, why doesnt this work?
For some reason the template
void swap(T *const element1Ptr, T *const element2Ptr)
doesnt work, anyone know why?.
Heres the code:
//Jason Project 3.
//This program puts values into an array, sorts the values into
//ascending order, and prints the resulting array using the template function bubbleSort.
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<fstream>
using namespace std;
//Template to Sort Arrays
template<class T>
void bubbleSort(T *array, const int size)
{
void swap(T *const, T *const);
for(int pass = 0;pass < size - 1;pass++)
for(int j = 0;j < size - 1;j++)
if(array[j] > array[j+1])
swap(&array[j], &array[j+1]);
}
template<class T>
void swap(T *const element1Ptr, T *const element2Ptr)
{
//Function swaps values of two variables for use with bubble sort.
T hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}
//Template to set Array Values.
template<class T>
void setarray(T *array, const int size)
{
for(int i = 0;i < size;i++)
array[i] = rand() & 999;
}
//Function Definitions
int getseed();
int main()
{
ofstream out("C:\\Driver.txt", ios::out);
const int arraySize = 100;
int i;
int a[arraySize];
float b[arraySize];
srand(getseed());
//Sets Values Into The Array.
setarray(a, arraySize);
setarray(b, arraySize);
//Outputs Heading For File.
out<< "Jason , Project 3, April 29, 2002"<< endl;
//Outputs integer Array Before and After Sorting.
out<< "Integer Array in original order\n";
for(i=0;i<arraySize;i++)
out<< setw(4)<<a[i];
bubbleSort(a, arraySize); //Sort the array.
out<< endl<< "Integer Array After Sorting"<< endl;
for(i=0;i<arraySize;i++)
out<<setw(4)<<a[i];
out<< endl;
//Outputs float Array Before and After Sorting.
out<< "Float Array in original order"<< endl;
for(i=0;i < arraySize;i++)
out<< setw(4)<<b[i];
bubbleSort(b, arraySize); //Sort the array.
out<< endl<< "Float Array After Sorting"<< endl;
for(i=0;i < arraySize; i++)
out<< setw(4)<< b[i];
out<< endl;
return 0;
}
int getseed()
{
//Function returns random value based on time and date.
//Used to return a better random value.
char temp;
int len, pos, seed;
string bob;
//Gets System Time
char tmp[80];
struct tm *ptr;
time_t lt;
lt = time(NULL);
ptr = localtime(<);
bob = asctime(ptr);
len = bob.length();
for(pos=0;pos<len;pos++){
temp = bob.at(pos);
if(isdigit(temp))
seed = seed + int(temp);}
return seed;
}