templated functions causing linker error

class mergeSort.cpp
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
#include <iostream>
#include <vector>
#include "mergeSortContainer.cpp"

using namespace std;

template void merge<vector<int>::iterator>(vector<int>::iterator begin, vector<int>::iterator mid, vector<int>::iterator end, bool (*compare)(int &x, int &y));

template void mergeSort<vector<int>::iterator>(vector<int>::iterator begin, vector<int>::iterator end, bool (*compare)(int &x, int &y));

bool exCompare(int &a, int &b);

bool exCompare(int &a, int &b) {
    return (a<b);
}

int main(){
    vector<int> vec;
    vec.push_back(5);
    vec.push_back(7);
    vec.push_back(6);
    vector<int>::iterator it = vec.begin();
    vector<int>::iterator it2 = vec.end();

    mergeSort<vector<int>::iterator>(it, it2, exCompare);
    return 0;
}


class mergeSortContainer.cpp:
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
#include <iostream>

using namespace std;

template <typename I>
void merge(I begin, I mid, I end, bool (*compare)(typename I::value_type &x, typename I::value_type &y)) {

    //Get the lengths of the left and the right part.
    int n1 = mid - begin;
    int n2 = end - mid;

    typedef typename I::value_type T;
    T* left = new T[n1];
    T* right = new T[n2];
    int i;

    //populate left and right arrays
    i = 0;
    for (I p=begin;i<n1;p++) {
        left[i] = *p;
        i++;
    }
    i = 0;
    for (I p=(mid);i<n2;p++) {
        right[i] = *p;
        i++;
    }
    

    T* result;
    i = 0;
    int leftCtr = 0;
    int rightCtr = 0;

    //Merge the two sorted arrays 
    for (;i<n1+n2;i++) {
        if (leftCtr<n1 && rightCtr<n2) {
            if (compare(left[leftCtr],right[rightCtr])) {
                result[i] = left[leftCtr++];
            }
            else result[i] = right[rightCtr++];
        }
        else if (leftCtr<n1)
            result[i] = left[leftCtr++];
        else
            result[i] = right[rightCtr++];
    }
    i = 0;

    //Copy the sorted array back into the original array.
    for (I p=begin;p!=end;p++) {
        *p = result[i++];
    }
}
            
            

template<typename I>
void mergeSort(I begin, I end, bool (*compare)(typename I::value_type &x, typename I::value_type &y)) {

    int len = end - begin;

    if (len>1) {
        I mid = begin + len/2;
        
        //Sort both the halves
        mergeSort(begin, mid, compare);
        mergeSort(mid, end, compare);
        merge(begin, mid, end, compare);
    }

    return;
} 


when I compile this program I get a long list of what appear to be linker errors. I am using a linux gcc compiler. Any ideas of how I can fix this?
Last edited on
You can't put templated functions in a .cpp file, they must be in the header file.
Ok, well I tried changing it to .h and am still getting the same linker error. Could you be more specific?
Alright I fixed the problem.
Last edited on
Topic archived. No new replies allowed.