0

I am trying to create a recursive mergesort application using 3 files: mergesort.cpp mergesort.h mergesort-test.cpp. I am getting an "undefined reference" error in my code and I am not sure why. The exact error I am receiving is this:

 /tmp/ccFPDpmn.o: In function `main':
 /home/cs104/hw-dendi/hw4/mergesort/msort-test.cpp:35: undefined reference to `void mergeSort<int, Compare>(std::vector<int, std::allocator<int> >&, Compare)'
 collect2: error: ld returned 1 exit status

I am using this command to compile: g++ -g -Wall -std=c++11 mergesort.cpp mergesort-test.cpp -o mergesort-test

Here are my files: mergesort.cpp

    #include <vector>
    #include "msort.h"
    using namespace std;

    template <class T, class Compare >
    void mergeSort(std::vector<T>& list, Compare comp){
        int start = 0;
        int end = list.size() - 1;
        recursive(list, comp, start, end);
    }

    template <class T, class Compare >
    void merge(std::vector<T>& list, Compare comp, int start, int end){
        std::vector<T> l(((start+end)/2)-start+1);
        std::vector<T> r(end - ((start+end)/2));

        for(int i = 0; i < l.size(); i++) {
            l[i] = list[start+i];
        }

        for(int i = 0; i < r.size(); i++) {
            r[i] = list[((start+end)/2) + 1 + i];
        }

        int i, j;
        while (i < l.size() && j < r.size()) {
            if (comp(l[i], r[j]) == true) {
                list[start] = l[i];
                l++;
            }
            else {
                list[start] = r[j];
                r++;
            }
            start++;
        }

        if (start != end) {
            while (i < l.size()) {
                list[start] = l[i];
                i++;
                start++;
            }
            while (j < r.size()) {
                list[start] = r[j];
                j++;
                start++;
            }
        }
    }

    template <class T, class Compare>
    void recursive(std::vector<T>& list, Compare comp, int start, int end) {
        recursive (list, comp, start, ((start+end)/2));
        recursive (list, comp, ((start+end)/2)+1, end);

        merge (list, comp, start, end);
    }

mergesort.h:

#include <vector>

template <class T, class Compare >
void mergeSort(std::vector<T>& list, Compare comp );

template <class T, class Compare >
void merge(std::vector<T>& list, Compare comp, int start, int end);

template <class T, class Compare>
void recursive(std::vector<T>& list, Compare comp, int start, int end);

struct Compare {
template <class T, class Compare>
    bool operator() (const T& left, const T& right) {
        return left > right;
    }
};

mergesort-test.cpp:

#include "msort.h"
#include <iostream>
#include <vector>
#include <functional>
#include <cstdlib>

using namespace std;

int main()
{
    vector <int> vec;
    vec.push_back(1);
    vec.push_back(3);
    vec.push_back(5);
    vec.push_back(7);
    vec.push_back(9);
    vec.push_back(2);
    vec.push_back(4);
    vec.push_back(6);
    vec.push_back(8);
    Compare c;
    mergeSort(vec, c);

for (int i = 0; i < vec.size(); i++) {
    cout << vec[i] << " ";
}

return 0;

}

nains
  • 199
  • 1
  • 2
  • 8
  • I looked at this post and could not find an answer that fixed my error. If anyone sees a part of that post that could help me, please let me know. Thank you for your response! – nains Apr 01 '20 at 01:49
  • 1
    https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file is the correct answer to your question. – Sam Varshavchik Apr 01 '20 at 01:51

0 Answers0