2017-04-26 36 views
-1

我一直收到的錯誤是 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常量&,INT常量&)) ' 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 to空隙歸併(標準::矢量> &,INT,INT,布爾()(浮動常量&,浮子常量&))' (std :: string)const &,std :: string const(int std :: string const &,std :: string const ))」 collect2:錯誤:LD返回1個退出狀態 的Makefile:13:配方目標 'assign8' 失敗 化妝:*** [assign8]錯誤1我的合併sort.h文件中導致未定義引用的代碼有什麼問題?

這裏是我的.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 

我找不到具體是由於某種原因導致錯誤。我知道這個錯誤非常小,如果可能,我需要另一雙眼睛來幫助。

+0

接受下面的答案,解決您的問題。 – mbaitoff

回答

0

函數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&)作爲參數。我想它應該有兩個參數而不是三個。

+0

非常感謝,解決了這個問題。我希望我能看到這一點 –