The error that I keep receiving is
g++ -Wall -std=c++11 -o assign8 assign8.o
assign8.o: In function void mergeSort<int>(std::vector<int, std::allocator<int> >&, bool (*)(int const&, int const&))':
assign8.cpp:(.text._Z9mergeSortIiEvRSt6vectorIT_SaIS1_EEPFbRKS1_S6_E[_Z9mergeSortIiEvRSt6vectorIT_SaIS1_EEPFbRKS1_S6_E]+0x37): undefined reference to
void mergeSort(std::vector >&, int, int, bool ()(int const&, int const&))'
assign8.o: In function void mergeSort<float>(std::vector<float, std::allocator<float> >&, bool (*)(float const&, float const&))':
assign8.cpp:(.text._Z9mergeSortIfEvRSt6vectorIT_SaIS1_EEPFbRKS1_S6_E[_Z9mergeSortIfEvRSt6vectorIT_SaIS1_EEPFbRKS1_S6_E]+0x37): undefined reference to
void mergeSort(std::vector >&, int, int, bool ()(float const&, float const&))'
assign8.o: In function void mergeSort<std::string>(std::vector<std::string, std::allocator<std::string> >&, bool (*)(std::string const&, std::string const&))':
assign8.cpp:(.text._Z9mergeSortISsEvRSt6vectorIT_SaIS1_EEPFbRKS1_S6_E[_Z9mergeSortISsEvRSt6vectorIT_SaIS1_EEPFbRKS1_S6_E]+0x37): undefined reference to
void mergeSort(std::vector >&, int, int, bool (*)(std::string const&, std::string const&))'
collect2: error: ld returned 1 exit status
makefile:13: recipe for target 'assign8' failed
make: *** [assign8] Error 1
Here is the code for my .cpp file which calls the template functions.
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include "sorts.h"
#include "quicksort.h"
#include "mergesort.h"
using std::cout;
using std::fixed;
using std::left;
using std::setprecision;
using std::string;
using std::vector;
// Data files
#define D1 "/home/turing/t90kjm1/CS241/Data/Spring2017/Assign8/data8a.txt"
#define D2 "/home/turing/t90kjm1/CS241/Data/Spring2017/Assign8/data8b.txt"
#define D3 "/home/turing/t90kjm1/CS241/Data/Spring2017/Assign8/data8c.txt"
// Output formatting constants
#define INT_SZ 4 // width of integer
#define FLT_SZ 7 // width of floating-pt number
#define STR_SZ 12 // width of string
#define INT_LN 15 // no of integers on single line
#define FLT_LN 9 // no of floating-pt nums on single line
#define STR_LN 5 // no of strings on single line
int main()
{
vector<int> v1; // vector of integers
vector<float> v2; // vector of floating-pt nums
vector<string> v3; // vector of strings
// Print header message
cout << "*** CSCI 241: Assignment 8 - Output ***\n\n";
// sort and print first list
cout << "First list - ascending order:\n\n";
buildList(v1, D1);
quickSort(v1, &lessThan);
printList(v1, INT_SZ, INT_LN);
v1.clear();
cout << "\nFirst list - descending order:\n\n";
buildList(v1, D1);
mergeSort(v1, &greaterThan);
printList(v1, INT_SZ, INT_LN);
// Sort and print second list
cout << fixed << setprecision(2);
cout << "\nSecond list - descending order:\n\n";
buildList(v2, D2);
quickSort(v2, &greaterThan);
printList(v2, FLT_SZ, FLT_LN);
v2.clear();
cout << "\nSecond list - ascending order:\n\n";
buildList(v2, D2);
mergeSort(v2, &lessThan);
printList(v2, FLT_SZ, FLT_LN);
// Sort and print third list
cout << left;
cout << "\nThird list - ascending order:\n\n";
buildList(v3, D3);
quickSort(v3, &lessThan);
printList(v3, STR_SZ, STR_LN);
v3.clear();
cout << "\nThird list - descending order:\n\n";
buildList(v3, D3);
mergeSort(v3, &greaterThan);
printList(v3, STR_SZ, STR_LN);
// print termination message
cout << "\n*** End of program execution ***\n";
return 0;
}
Here is the code for my merge sort.h file.
#ifndef MERGESORT_H
#define MERGESORT_H
#include <vector>
#include <iostream>
using std::vector;
template <class T> void mergeSort(vector<T>&, bool (*)(const T&, const T&));
template <class T> void mergeSort(vector<T>&, int, int, bool (*)(const T&, const T&));
template <class T> void merge(vector<T>&, int, int, int, bool (*)(const T&, const T&));
template <class T>
void mergeSort(vector<T>& set, bool (*compare)(const T&, const T&))
{
mergeSort(set, 0, set.size()-1, compare);
}
template <class T>
void mergeSort(vector<T>& set, int low, int high, bool (*compare)(const T&, const T&, const T&))
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
// Divide and conquer
mergeSort(set, low, mid, compare);
mergeSort(set, mid + 1, high, compare);
//Combine
merge(set, low, mid, high, compare);
}
}
template <class T>
void merge(vector<T>& set, int low, int mid, int high, bool (*compare)(const T&, const T&))
{
vector<T> temp(high - low + 1);
int i = low; //Subscript for start of left sorted subvector
int j = mid + 1; // Subscript for start of right sorted subvector
int k = 0; // Subscript for start of merged vector
// While not at the end of either subvector
while (i <= mid && j <= high)
{
if (compare(set[j], set[i]))
{
temp[k] = set[j];
j++;
k++;
}
else
{
temp[k] = set[i];
i++;
k++;
}
}
// Copy over any remaining elements of left subvector
while (i <= mid)
{
temp[k] = set[i];
i++;
k++;
}
// Copy over any remaining elements of right subvector
while (j <= high)
{
temp[k] = set[j];
j++;
k++;
}
// Copy merged elements back into original vector
for (i = 0, j = low; j <= high; i++, j++)
{
temp[i] = set[j];
}
}
#endif
I can't find what specifically is causing the error for some reason. I know that the error is really small and I need another set of eyes to help if possible.