尝试编译时出现此错误:
psort.cpp:11:17: error: variable or field ‘psort’ declared void
void psort(std::vector<T> * array)
我不确定我为什么会得到这个。我阅读了其他一些关于相同错误的帖子,但它们并没有真正帮助我。
/**
* @file psort.h
*/
#ifndef _PSORT_H_
#define _PSORT_H_
#include <iostream>
#include <vector>
#include <omp.h>
using std::vector;
namespace ParallelSort
{
/**
* Public wrapper for the parallel sort implementation.
* @param array The structure to sort
*/
template <class T>
void psort(vector<T> * array);
template <class T>
vector<T> * mergeSort(vector<T> * array, int left, int right);
template <class T>
vector<T> * merge(vector<T> * arr1, vector<T> * arr2);
}
#include "psort.cpp"
#endif
这是 .cpp :
template <class T>
void psort(vector<T> * array)
{
mergeSort(array, 0, array->size() - 1);
}
template <class T>
vector<T> * mergeSort(vector<T> * array, int left, int right) {
if (left >= right) {
return array;
}
int midpoint = (left + right) / 2;
return merge(mergeSort(array, left, midpoint - 1), mergeSort(array, midpoint, right));
}
template <class T>
vector<T> * merge(vector<T> * arr1, vector<T> * arr2) {
vector<T> * result();
result->resize(arr1->size() + arr2->size());
int i = 0; // result array position
int j = 0; // arr1 position
int k = 0; // arr2 position
while (i <result->size() ) {
if (j >= arr1->size()) {
result[i] = arr2[k];
i++;
k++;
} else if (k >= arr2->size()) {
result[i] = arr1[j];
i++;
j++;
} else if (arr1[j] <= arr2[k]) {
result[i] = arr1[j];
i++;
j++;
} else if (arr2[k] <= arr1[j]) {
result[i] = arr2[k];
i++;
k++;
}
}
return result;
}