【发布时间】:2020-08-23 00:46:06
【问题描述】:
我正在尝试通过向量调用厨师类的Thread,但我不知道如何正确执行。这是我的代码:
for (int i = 0; i < _numCooks; i++) {
std::thread threadObj(this->_cooks[i].nextOrder, void);
_threads.push_back(threadObj);
}
_cooks 是一个类的向量,我试图创建一个线程的函数是:
bool cooks::nextOrder()
{
Margarita margarita(_cookOrder[0].pizzaSize);
Regina regina(_cookOrder[0].pizzaSize);
Fantasia fantasia(_cookOrder[0].pizzaSize);
Americana americana(_cookOrder[0].pizzaSize);
if (_cookOrder.empty())
return (false);
switch (_cookOrder[0].pizzaType) {
case plazza::Margarita:
return (makeOrder(margarita));
break;
case plazza::Regina:
return (makeOrder(regina));
break;
case plazza::Fantasia:
return (makeOrder(fantasia));
break;
case plazza::Americana:
return (makeOrder(americana));
break;
default:
return (false);
}
}
但是当我尝试编译时,我得到了这个:
kitchen/kitchen.cpp: 在成员函数‘void Kitchen::make_pizza(std::__cxx11::string, std::__cxx11::string, Ipc)’: kitchen/kitchen.cpp:148:59:错误:预期的主要表达式之前 '空白' std::thread threadObj(this->_cooks[i].nextOrder, void); ^~~~ kitchen/kitchen.cpp:148:63: 错误:非静态成员的使用无效 函数‘bool cooks::nextOrder()’ std::thread threadObj(this->_cooks[i].nextOrder, void); ^ 包含在 kitchen/kitchen.hpp:17 中的文件中, 来自 kitchen/kitchen.cpp:5: kitchen/cooks.hpp:32:10: 注意:在此声明 布尔下一个订单(); ^~~~~~~~~ 在 /usr/include/c++/8/x86_64-redhat-linux/bits/c++allocator.h:33 包含的文件中, 来自 /usr/include/c++/8/bits/allocator.h:46, 来自 /usr/include/c++/8/string:41, 来自 /usr/include/c++/8/bits/locale_classes.h:40, 来自 /usr/include/c++/8/bits/ios_base.h:41, 来自 /usr/include/c++/8/ios:42, 来自 /usr/include/c++/8/ostream:38, 来自 /usr/include/c++/8/iostream:39, 从厨房/../pizza/IPizza.hpp:23, 从厨房/../pizza/APizza.hpp:11, 从厨房/厨房.hpp:8, 从 kitchen/kitchen.cpp:5: /usr/include/c++/8/ext/new_allocator.h: 在“void”的实例化中 __gnu_cxx::new_allocator<_tp>::construct(_Up*, _Args&& ...) [with _Up = cooks; _Args = {常量厨师&}; _Tp = cooks]': /usr/include/c++/8/bits/alloc_traits.h:475:4: 来自'static 无效 std::allocator_traits ::construct(std::allocator_traits >::allocator_type&, _Up*, _Args&& ...) [with _Up = cooks; _Args = {常量厨师&}; _Tp = 厨师; std::allocator_traits >::allocator_type = std::allocator]’ /usr/include/c++/8/bits/stl_vector.h:1079:30: 'void std::vector<_tp _alloc>::push_back(const value_type&) [with _Tp = cooks; _Alloc = std::allocator; std::vector<_tp _alloc>::value_type = cooks]’ kitchen/kitchen.cpp:36:30:从这里需要 /usr/include/c++/8/ext/new_allocator.h:136:4:错误:使用已删除 函数‘cooks::cooks(const cooks&)’ { ::new((void )__p) _Up(std::forward<_args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~ 在 kitchen/kitchen.hpp:17 包含的文件中, 从 kitchen/kitchen.cpp:5: kitchen/cooks.hpp:21:7: note: ‘cooks::cooks(const cooks&)’ 被隐式删除,因为 默认定义格式不正确:class cooks ^~~~~ kitchen/cooks.hpp:21:7: 错误:使用已删除的函数‘std::basic_ofstream<_chart _traits>::basic_ofstream(const std::basic_ofstream<_chart _traits>&) [with _CharT = char; _特质= std::char_traits]’ 在包含的文件中 厨房/../plazza.hpp:15, 从厨房/厨房.hpp:13, 来自 kitchen/kitchen.cpp:5: /usr/include/c++/8/fstream:778:7: 注意:在此处声明 basic_ofstream(const basic_ofstream&) = 删除; ^~~~~~~~~~~~~~ 在 /usr/include/c++/8/vector:62 包含的文件中, 从厨房/../pizza/IPizza.hpp:24, 从厨房/../pizza/APizza.hpp:11, 从厨房/厨房.hpp:8, 来自 kitchen/kitchen.cpp:5: /usr/include/c++/8/bits/stl_construct.h: 在“void”的实例化中 std::_Construct(_T1, _Args&& ...) [with _T1 = cooks; _Args = {厨师}]’:/usr/include/c++/8/bits/stl_uninitialized.h:83:18:
需要从'静态_ForwardIterator std::__uninitialized_copy<_trivialvaluetypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator; _ForwardIterator = 厨师*;布尔 _TrivialValueTypes = false]’ /usr/include/c++/8/bits/stl_uninitialized.h:134:15:需要来自 ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator; _ForwardIterator = 厨师*]’ /usr/include/c++/8/bits/stl_uninitialized.h:289:37:需要从 ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_tp>&) [with _InputIterator = std::move_iterator; _ForwardIterator = 厨师*; _Tp = 厨师]’ /usr/include/c++/8/bits/stl_uninitialized.h:311:2:需要从 '_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = cooks*; _ForwardIterator = 厨师*; _分配器= std::allocator]’ /usr/include/c++/8/bits/vector.tcc:447:6:
来自‘void std::vector<_tp _alloc>::_M_realloc_insert(std::vector<_tp _alloc>::iterator, _Args&& ...) [with _Args = {const cooks&}; _Tp = 厨师; _分配= 标准::分配器; std::vector<_tp _alloc>::iterator = __gnu_cxx::__normal_iterator >;类型名 std::_Vector_base<_tp _alloc>::pointer = cooks*]’ /usr/include/c++/8/bits/stl_vector.h:1085:4: 需要来自‘void std::vector<_tp _alloc>::push_back(const value_type&) [with _Tp = 厨师; _Alloc = std::allocator;标准::向量<_tp _alloc>::value_type = cooks]' kitchen/kitchen.cpp:36:30: 这里需要 /usr/include/c++/8/bits/stl_construct.h:75:7: 错误:使用 删除函数‘cooks::cooks(const cooks&)’ { ::new(static_cast(__p)) _T1(std::forward<_args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ make: *** [: kitchen/kitchen.o] 错误 1
有什么办法可以解决吗?
【问题讨论】:
-
一些编译错误是由于
cooks不可复制。确保您了解en.cppreference.com/w/cpp/language/rule_of_three 并知道cooks成员是否可复制
标签: c++ multithreading compiler-errors