私が受け取っているエラーは、 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 to
void 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 to
無効マージソート(のstd ::ベクトル> &、int型、int型、ブール値()(フロートconstの&、フロートの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 to
void mergeSort(std :: vector> &、int、int、bool(*)(std ::文字列const &、std ::文字列const &))」 collect2は:エラー:13:ldは1つの終了ステータス メイクファイルを返されたターゲットのためのレシピ '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
何らかの理由でエラーの原因となる具体的な原因が見つかりません。私はエラーが本当に小さいことを知っています、可能ならば私は助けてくれる別の目が必要です。
あなたの問題を解決した以下の回答を受け入れてください。 – mbaitoff