导致未定义引用的合并 sort.h 文件中的代码有什么问题?

What is wrong with my code within my merge sort.h file that is causing a undefined reference?

我一直收到的错误是 g++ -Wall -std=c++11 -o assign8 assign8.o assign8.o: 在函数 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 tovoid mergeSort(std::vector >&, int, int, bool ()(int const&, int const&))' assign8.o: 在函数 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 tovoid mergeSort(std::vector >&, int, int, bool ()(float const&, float const&))' assign8.o: 在函数 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 tovoid mergeSort(std::vector >&, int, int, bool (*)(std::string const&, std::string const&))' collect2:错误:ld 返回 1 退出状态 makefile:13:目标 'assign8' 的配方失败 make: *** [assign8] 错误 1

这是调用模板函数的 .cpp 文件的代码。

#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;
   }

这是我的合并 sort.h 文件的代码。

#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

出于某种原因,我找不到具体导致错误的原因。我知道误差真的很小,如果可能的话,我需要另一双眼睛来帮忙。

函数void mergeSort(std::vector >&, int, int, bool (*)(int const&, int const&))未定义。

同时,这个函数的定义

void mergeSort(vector<T>& set, int low, int high, bool (*compare)(const T&, const T&, const T&))

接受一个 bool (*)(const T&, const T&, const T&) 作为参数。我想它应该采用两个参数而不是三个参数。