gpt4 book ai didi

c++ - 对线程C++使用类数组的奇怪错误

转载 作者:行者123 更新时间:2023-12-02 10:56:39 24 4
gpt4 key购买 nike

我正在尝试通过 vector 调用Cookt类的Thread,但我不知道该怎么做。这是我的代码:

for (int i = 0; i < _numCooks; i++) {
std::thread threadObj(this->_cooks[i].nextOrder, void);
_threads.push_back(threadObj);
}

_cooks是一个类的 vector ,而我试图作为其线程的函数是:
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: In member function ‘void Kitchen::make_pizza(std::__cxx11::string, std::__cxx11::string, Ipc)’: kitchen/kitchen.cpp:148:59: error: expected primary-expression before ‘void’ std::thread threadObj(this->_cooks[i].nextOrder, void); ^~~~ kitchen/kitchen.cpp:148:63: error: invalid use of non-static member function ‘bool cooks::nextOrder()’ std::thread threadObj(this->_cooks[i].nextOrder, void); ^ In file included from kitchen/kitchen.hpp:17, from kitchen/kitchen.cpp:5: kitchen/cooks.hpp:32:10: note: declared here bool nextOrder(); ^~~~~~~~~ In file included from /usr/include/c++/8/x86_64-redhat-linux/bits/c++allocator.h:33, from /usr/include/c++/8/bits/allocator.h:46, from /usr/include/c++/8/string:41, from /usr/include/c++/8/bits/locale_classes.h:40, from /usr/include/c++/8/bits/ios_base.h:41, from /usr/include/c++/8/ios:42, from /usr/include/c++/8/ostream:38, from /usr/include/c++/8/iostream:39, from kitchen/../pizza/IPizza.hpp:23, from kitchen/../pizza/APizza.hpp:11, from kitchen/kitchen.hpp:8, from kitchen/kitchen.cpp:5: /usr/include/c++/8/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = cooks; _Args = {const cooks&}; _Tp = cooks]’: /usr/include/c++/8/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits ::construct(std::allocator_traits >::allocator_type&, _Up*, _Args&& ...) [with _Up = cooks; _Args = {const cooks&}; _Tp = cooks; std::allocator_traits >::allocator_type = std::allocator]’ /usr/include/c++/8/bits/stl_vector.h:1079:30: required from ‘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: required from here /usr/include/c++/8/ext/new_allocator.h:136:4: error: use of deleted function ‘cooks::cooks(const cooks&)’ { ::new((void )__p) _Up(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from kitchen/kitchen.hpp:17, from kitchen/kitchen.cpp:5: kitchen/cooks.hpp:21:7: note: ‘cooks::cooks(const cooks&)’ is implicitly deleted because the default definition would be ill-formed: class cooks ^~~~~ kitchen/cooks.hpp:21:7: error: use of deleted function ‘std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const std::basic_ofstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits]’ In file included from kitchen/../plazza.hpp:15, from kitchen/kitchen.hpp:13, from kitchen/kitchen.cpp:5: /usr/include/c++/8/fstream:778:7: note: declared here basic_ofstream(const basic_ofstream&) = delete; ^~~~~~~~~~~~~~ In file included from /usr/include/c++/8/vector:62, from kitchen/../pizza/IPizza.hpp:24, from kitchen/../pizza/APizza.hpp:11, from kitchen/kitchen.hpp:8, from kitchen/kitchen.cpp:5: /usr/include/c++/8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1, _Args&& ...) [with _T1 = cooks; _Args = {cooks}]’: /usr/include/c++/8/bits/stl_uninitialized.h:83:18:
required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator; _ForwardIterator = cooks*; bool _TrivialValueTypes = false]’ /usr/include/c++/8/bits/stl_uninitialized.h:134:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator; _ForwardIterator = cooks*]’ /usr/include/c++/8/bits/stl_uninitialized.h:289:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator; _ForwardIterator = cooks*; _Tp = cooks]’ /usr/include/c++/8/bits/stl_uninitialized.h:311:2: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = cooks*; _ForwardIterator = cooks*; _Allocator = std::allocator]’ /usr/include/c++/8/bits/vector.tcc:447:6:
required from ‘void std::vector<_Tp, _Alloc>::_M_realloc_insert(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {const cooks&}; _Tp = cooks; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator >; typename std::_Vector_base<_Tp, _Alloc>::pointer = cooks*]’ /usr/include/c++/8/bits/stl_vector.h:1085:4: required from ‘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: required from here /usr/include/c++/8/bits/stl_construct.h:75:7: error: use of deleted function ‘cooks::cooks(const cooks&)’ { ::new(static_cast(__p)) _T1(std::forward<_Args>(__args)...); } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ make: *** [: kitchen/kitchen.o] Error 1



有什么办法可以解决?

最佳答案

根据thisstd::thread构造函数的第一个参数应满足Callable概念。然后,将Callable的后续参数作为附加参数传递给构造函数。

您正在传递成员函数。您需要一个类的实例来调用您未提供的方法。这就是该错误对您的含义。

error: invalid use of non-static member function ‘bool cooks::nextOrder()’

使用lambda即是最简单的方法。
for (int i = 0; i < _numCooks; i++) {
_threads.emplace_back([&cook = _cooks[i]]() {
cook.nextOrder();
});
}

关于c++ - 对线程C++使用类数组的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61662580/

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