我想写一个小程序来学习C++ 11多线程程序。所以我决定写一个mergeSort来测试它。C++ 11多线程合并排序错误“没有构造函数的实例std :: thread'匹配参数列表”
这里是我的代码:
class SortMethods
{
protected:
int N;
int *aux;
public:
void mergeSort(int a[])
{
aux = new int[N];
mergeSort(a, 0, N - 1);
}
void merge(int a[], int low, int high)
{
int mid = (low + high)/2;
//optimization 3 for nearly-sorted array
//we can add a condition to improve performance when the array has already sorted or nearly-sorted.
if (a[mid] <= a[mid + 1])
{
return;
}
int i = low;
int j = mid + 1;
for (int k = low; k <= high; k++)
{
aux[k] = a[k];
}
for (int k = low; k <= high; k++)
{
if (i > mid)
a[k] = aux[j++];
else if (j > high)
a[k] = aux[i++];
else if (lessThan(aux[j], aux[i]))
a[k] = aux[j++];
else
a[k] = aux[i++];
}
}
void mergeSort(int a[], int low, int high)
{
if (high <= low)
{
return;
}
int mid = low + (high - low)/2;
//single_thread
mergeSort(a, low, mid);
mergeSort(a, mid + 1, high);
/*merge(a, low, high);*/
//multi_thread
/*thread left(mergeSort, a, low, mid);
thread right(mergeSort, a, mid + 1, high);
left.join();
right.join();*/
merge(a, low, high);
}
}
int main()
{
int *a = new int(100);
for(int i=0; i<100; i++)
{
a[i] = rand() % 1000;
}
SortMethods sort(100);
sort.mergeSort(a);
}
但是,当我在编译的VS2015的代码,它会抛出一个没有构造“的std ::线程”的实例参数列表相匹配的错误。
你能帮我找到我的代码问题吗?
=============================================
因为你们的帮助,我发现,因为我重载mergeSort方法。我重命名方法mergeSort_multi_thread
thread left(&SortMethods::mergeSort_multi_thread, a, low, mid);
我得到了错误 没有专门的函数模板“未知类型的std ::调用(_Callable & &,_types & & ......)
1> g:\dataalog\datastructuresandalgo\basic_data_structures\sorting.h(240): note: see reference to function template instantiation 'std::thread::thread<void(__thiscall SortMethods::*)(int [],int,int),int&[],int&,int&,void>(_Fn &&,int &[],int &,int &)' being compiled
1> with
1> [
1> _Fn=void (__thiscall SortMethods::*)(int [],int,int)
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
[enter image description here][1]
Thx
您正在使用方法,而不是函数。检查此答案http://stackoverflow.com/questions/10673585/start-thread-with-member-function了解如何传递成员函数及其各自的对象 – Nadir
不要评论导致错误的代码,因为否则人们无法重现错误!发布不起作用的代码!但是,这已经被答复了一百万次。 –