C++线程中,没有重载函数接受 X 参数
C++ thread no overloaded function takes X arguments
我在程序中启动线程时遇到问题。我有一个类,看起来像这样:
class quicksort {
private:
// Array parameters
int length;
// Actual sorting functions
template <typename T>
void _sort(T* data, int, int);
template <typename T>
int _partition(T* data, int, int);
template <typename T>
void _swap(T* data, int, int);
void test_partition(int* data, int length);
public:
// Constructors
quicksort() {}
// Sorting functions
template <typename T>
void sort(T* data, int len);
void test();
};
_sort()
方法如下所示:
template <typename T>
void quicksort::_sort(T* data, int p, int r) {
if (p < r) {
auto q = _partition(data, p, r);
std::thread lower(&quicksort::_sort, this, data, p, q - 1);
std::thread upper(&quicksort::_sort, this, data, q + 1, r);
lower.join();
upper.join();
}
}
当我编译它时,我收到此错误:
C:UsersFrynioDropboxStudiaZSSKProjektquicksortinclude/quicksort.hpp(55): error C2661: 'std::thread::thread': no overloaded function takes 5 arguments
C:UsersFrynioDropboxStudiaZSSKProjektquicksortinclude/quicksort.hpp(41): note: see reference to function template instantiation 'void quicksort::_sort<T>(T *,int,int)' being compiled
with
[
T=int
]
../src/main.cpp(8): note: see reference to function template instantiation 'void quicksort::sort<int>(T *,int)' being compiled
with
[
T=int
]
C:UsersFrynioDropboxStudiaZSSKProjektquicksortinclude/quicksort.hpp(56): error C2661: 'std::thread::thread': no overloaded function takes 5 arguments
55 和 56 是我开始线程的行。我似乎不明白我做错了什么。我认为参数传递是可以的,所以我认为问题可能是,data
是T
类型,这是一个模板方法。是这样吗?如果是,有没有办法解决它?
我想你的意思是写(用<T>
(
std::thread lower(&quicksort::_sort<T>, this, data, p, q - 1);
std::thread upper(&quicksort::_sort<T>, this, data, q + 1, r);
否则,编译器将如何推断要传递给std::thread
构造函数的哪个_sort
实例化?事实上,当我使用 clang 7.0 编译您的代码时,我收到以下附加错误:
thread:118:7: note: candidate template ignored: couldn't infer template argument '_Callable'
thread(_Callable&& __f, _Args&&... __args)
^
因此,这表明它无法确定&quicksort::sort
的类型。
相关文章:
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 当从函数参数中的临时值调用复制构造函数时
- 如何从"decltype()"获取函数参数的数量<funtion>?
- 如何将lambda作为模板类的成员函数参数
- 模板参数推导失败,函数参数/参数不匹配
- 如何在C++中将迭代器作为函数参数传递
- 将函数参数"const char*"转换为"std::string_view"是
- C++ 如何将数组值解压缩为函数参数
- 主函数参数的属性
- 具有两个间接寻址运算符 (C++) 的函数参数的用途
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何定义在用作函数参数时工作的类模板的转换
- 将函数参数完美转发到函数指针:按值传递呢?
- 为什么我不能将引用作为 std::async 的函数参数传递
- 什么..(省略号)作为函数原型中唯一的函数参数,C++?
- 是否可以就地构造一个固定大小的数组作为函数参数?
- 接受模板作为函数参数
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- Arduino 函数参数