bubble sort template

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.

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
  #include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

template <typename T>
T bubbleSort(T arr[], T n);
template <typename T>
T print(T arr[]);

const int 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.
Your sort and print methods should be void seeing as they don't return anything
I know that, I only changed them to T to try different things to make it work. With void I still get the same error.
i think as you've defined it like this:

T bubbleSort(T arr[], T n)

When you write:
bubbleSort<string>(strings, MAX);

it's having problems with you MAX, as that's an integer, but your template is saying it's a string.

Don't think i've explained that very well sorry.

in other words you have:
std::string bubbleSort(std::string arr[], std::string n)

but you're passing in an int for n.
Last edited on
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.
you're welcome :)
incidentally, it was my compiler that told me:

error C2664: 'bubbleSort' : cannot convert parameter 2 from 'const int' to 'std::string'
Last edited on
Topic archived. No new replies allowed.