I am writing a program that uses a template to take in ints, doubles and strings from files and sorts them using bubble sort. Right now the ints and doubles are printed fine but the problem comes with the string.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
usingnamespace std;
template <typename T>
T bubbleSort(T arr[], T n);
template <typename T>
T print(T arr[]);
constint MAX = 20;
int main()
{
int integers[MAX];
ifstream intInput("C:/Users/Gregory/Desktop/integers.txt");
//print out ints ordered (low to high)
while (!intInput.eof()) {
for (int i=0; i< MAX; i++)
intInput >> integers[i];
}
bubbleSort<int>(integers, MAX);
print<int>(integers);
double doubles[MAX];
ifstream doubleInput("C:/Users/Gregory/Desktop/doubles.txt");
while (!doubleInput.eof()){
for (int i=0;i<MAX;i++)
doubleInput >> doubles[i];
}
bubbleSort<double>(doubles, MAX);
cout << endl;
print<double>(doubles);
string strings[MAX];
ifstream stringInput("C:/Users/Gregory/Desktop/strings.txt");
while (!stringInput.eof()) {
for (int i = 0;i<MAX; i++)
stringInput >> strings[i];
}
bubbleSort<string>(strings, MAX);
cout << endl;
print<string>(strings);
}
template <typename T>
T bubbleSort(T arr[], T n)
{
T temp;
bool swap;
do
{
swap = false;
for (int count = 0; count < (n - 1); count++)
{
if (arr[count] > arr[count + 1])
{
temp = arr[count];
arr[count] = arr[count + 1];
arr[count + 1] = temp;
swap = true;
}
}
}while (swap);
}
template <typename T>
T print (T arr[])
{
for (int i=0; i<MAX; i++)
cout << arr[i] << " ";
}
Compiler is telling me on line 43 that there is no matching call to bubblesort. I assume it's a problem with converting strings correctly into the sort but not sure how to fix it. Any help would be appreciated.
mutexe, thanks that was the problem. completely forgot that it would be expecting a string in both cases when I instantiate the template to expect strings.