首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对线程C++使用类数组时出现奇怪的错误

对线程C++使用类数组时出现奇怪的错误
EN

Stack Overflow用户
提问于 2020-05-07 16:11:19
回答 1查看 892关注 0票数 0

我试图做一个Thread,通过向量调用厨师类,但我不知道如何正确。这是我的代码:

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

_cooks是一个类的向量,而我正在尝试创建一个线程的函数是:

代码语言:javascript
复制
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);

    }
}

然而,当我试图编译时,我得到了以下内容:

厨房/厨房.Ipc:成员函数‘void Kitchen::make_pizza(std::__cxx11::string,std::__cxx11::string,Ipc)’:厨房/厨房.Ipc:148:59:错误:在‘void’std::线程threadObj之前的预期主表达式(this->_gusi.nextOrder,void);^~厨房/厨房.std:148:63:错误:无效使用非静态成员函数‘bool cooks::nextOrder()’std::threadObj线程(this->_cosi.nextOrder,void);在文件中包含在厨房/厨房. here :17,厨房/厨房. bool :5:厨房/炊具.here:32:10:注意:声明这里是bool nextOrder();^~在/usr/include/c++/8/x86_64-redhat-linux/bits/c++allocator.h:33,文件中包含:/usr/include/c++/8/bit/allocator.h:46,from /usr/include/c++/8/string:41,from /usr/include/c++/8/bit/locale_classes.h:40,见/usr/include/8/bit/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,从厨房/./比萨/IPizza.hpp:23,从厨房/./比萨/apizza.hpp:11,从厨房/厨房.new:8,从厨房/厨房.c:5:/usr/include/c++/8/ext/新_allocator.h:实例化‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*,_Args&& .’)使用_Up =厨师;_Args = {const &};_Tp =cooks‘:/usr/include/c++/8/bit/alloc_traits.h:475:4: required from’Staticvoidstd::allocator_traits::static( std::allocator_traits >::allocator_type&,_Up*,_Args&& .)_Up = cooks;_Args = {const &};_Tp = cooks;std::allocator_traits >::allocator_type =std::allocator‘/usr/include/c++/8/bit/stl_vector.h:1079:30:从’void std::vector<_Tp,_Alloc>::push_back(const value_type&)到_Tp = cooks;_Alloc = std::allocator;std::vector<_Tp,_Alloc>::value_type =厨师的厨房/厨房:_Up:36:30:必需从这里/usr/include/c++/8/ext/new_allocator.h:136:4:错误:使用已删除的函数‘厨师::厨师(康斯特厨师&)’{::新((无效)__p) _Up(std::forward<_Args>(__args).);} ^~文件包含在厨房/厨房.ill:17,厨房/厨房.ill:5:厨房/炊具.ill:21:7:注意:‘厨师::厨师(康斯特厨师&)’是隐式删除的,因为默认定义是错误的:类厨师^厨房/厨房:21:7:错误:使用已删除的函数‘std::basic_ofstream<_CharT,_Traits>::basic_ofstream(const std::basic_ofstream<_CharT,( _Traits>&)与_CharT = char;_Traits =std::char_traits‘包含在厨房/./plazza.hpp:15,厨房/厨房.778:13,厨房/厨房.c:5: /usr/include/c++/8/fstream:778:7:注意: basic_ofstream(const basic_ofstream&) =删除;^~文件包含于/usr/include/c++/8/载体:62,来自厨房/./比萨/IPizza.hpp:24,来自厨房/./比萨/apizza.hpp:11,来自厨房/厨房.usr:8,从厨房/厨房.c:5:/usr/include/c++/8/bit/stl_struct.h:在‘void std::_Construct(_T1,_Args&& .与_T1 = cooks;_Args ={cooks}‘:_Args 需要从“静态std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator,/usr/include/c++/8/bits/stl_uninitialized.h:134:15:,_ForwardIterator)和_InputIterator = std::move_iterator;_ForwardIterator = cooks*;bool _TrivialValueTypes =false‘_ForwardIterator std::uninitialized_copy( _InputIterator,_InputIterator,_ForwardIterator)到_InputIterator= std::move_iterator;/usr/include/c++/8/bits/stl_uninitialized.h:289:37::_ForwardIterator std::__uninitialized_copy_a( _InputIterator,_InputIterator,_ForwardIterator,std::allocator<_Tp>&)中的_InputIterator= std::move_iterator;_ForwardIterator = cooks*;从‘/usr/include/c++/8/bits/stl_uninitialized.h:311:2: std::__uninitialized_move_if_noexcept_a(_InputIterator,_InputIterator,_ForwardIterator,_Allocator&)到_InputIterator = cooks*;_ForwardIterator = cooks*;_Allocator =std::allocator’/usr/include/c++/8/bit/vector.tcc:447:6: 需要从‘void std::vector<_Tp,_Alloc>::_M_realloc_insert(std::vector<_Tp,_Alloc>::iterator,_Args&& ._Args ={ cooks&};_Tp = cooks;_Alloc =std::分配器;std::vector<_Tp,_Alloc>::iterator = __gnu_cxx::__normal_iterator >;类型名称std::_Vector_base<_Tp,_Alloc>::pointer =cooks*‘/usr/_Alloc>::pointer/c++/8/bit/stl_vector.h:1085:4:从’void std::vector<_Tp,_Alloc>::push_back(const value_type&)到_Tp = cooks;_Alloc =std:分配程序;std::vector<_Tp,_Alloc>::value_type =厨师的厨房/厨房。__p:36:30:必需从这里/usr/include/c++/8/bit/stl_struct.h:75:7:错误:使用删除的函数‘厨师::厨师(康斯特厨师&)’{::新的(static_cast(__p))_T1(std::forward<_Args>(__args).);} ^~ make:*:厨房/厨房

有办法解决吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-07 16:32:07

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

你在传递一个成员函数。您需要类的一个实例来调用方法,而您并没有提供这个实例。这就是这个错误对你意味着什么。

代码语言:javascript
复制
error: invalid use of non-static member function ‘bool cooks::nextOrder()’

这可能是最简单的使用羔羊。

代码语言:javascript
复制
for (int i = 0; i < _numCooks; i++) {
    _threads.emplace_back([&cook = _cooks[i]]() {
        cook.nextOrder();
     });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61662580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档