gpt4 book ai didi

C++ 编译器错误 'no match for call to function'

转载 作者:行者123 更新时间:2023-11-28 00:42:03 25 4
gpt4 key购买 nike

我目前正在尝试学习 C++,但遇到了一个我不理解的编译器错误。有人可以解释编译器错误的含义吗?

 1 #include <thread>
2 #include <vector>
3 #include <iostream>
4
5 namespace thread_pool {
6
7 class worker {
8 public:
9 worker();
10 private:
11 };
12
13 worker::worker() { std::cout << "hello from worker\n"; }
14
15 class pool_keeper {
16 public:
17 pool_keeper(int);
18 private:
19 std::vector< std::thread > workers_;
20 int pool_size_;
21 };
22
23 pool_keeper::pool_keeper(int pool_size) {
24 int i;
25 pool_size_ = pool_size;
26 for (i=0; i<pool_size_; i++)
27 workers_.push_back(std::thread(worker()));
28 }
29 }

编译器给我这个:

In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/thread:39:0,
from 01.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/functional: In member function ‘void std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__call(std::tuple<_Args ...>&&, std::_Index_tuple<_Indexes ...>, typename std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__enable_if_void<_Res>::type) [with _Res = void, _Args = {}, int ..._Indexes = {}, _Result = void, _Functor = thread_pool::worker, _Bound_args = {}, typename std::_Bind_result<_Result, _Functor(_Bound_args ...)>::__enable_if_void<_Res>::type = int]’:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/functional:1378:24: instantiated from ‘std::_Bind_result<_Result, _Functor(_Bound_args ...)>::result_type std::_Bind_result<_Result, _Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {}, _Result = void, _Functor = thread_pool::worker, _Bound_args = {}, std::_Bind_result<_Result, _Functor(_Bound_args ...)>::result_type = void]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/thread:117:13: instantiated from ‘void std::thread::_Impl<_Callable>::_M_run() [with _Callable = std::_Bind_result<void, thread_pool::worker()>]’
01.cpp:29:1: instantiated from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include/g++-v4/functional:1287:4: error: no match for call to ‘(thread_pool::worker) ()’

我试图通过查看 std::thread 的代码来理解编译器错误的含义,但我认为有更好的方法可以找到解决方案。

最佳答案

该消息表示编译器不知道如何调用thread_pool::worker 类型的对象。 std::thread 构造函数需要一个可调用对象,即函数或定义了 operator() 的对象。

解决方法是将工作从 thread_pool::worker 的构造函数移至 operator()()

关于C++ 编译器错误 'no match for call to function',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18426167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com