首页 文章

错误:移动承诺时“尝试使用已删除的功能”

提问于
浏览
0

我正在尝试写一个带有诺言/未来的例子 . 在this reference page中的示例中,他们这样做,并且它的在线编译器工作得很好 . 当我在我的Mac上执行它(源代码在下面)时,它会抛出这个错误,表明没有移动操作符 . 谁能告诉我有什么问题?

头文件:

#ifndef Demonstration9_hpp
#define Demonstration9_hpp

#include <iostream>
#include <thread>
#include <future>

using namespace std;

inline namespace demo9
{
    class Demonstration9
    {
    private:
        bool Job1();
        void Job2();

    public:
        void Run();
    };
}

#endif /* Demonstration9_hpp */

源文件:

#include "Demonstration9.hpp"

bool job1(string test_string)
{
    this_thread::sleep_for(chrono::milliseconds(500));
    return test_string == "test";
}

void job2()
{
    this_thread::sleep_for(chrono::milliseconds(500));
}

bool Demonstration9::Run()
{
    promise<bool> j1_promise;
    future<bool> j1_future = j1_promise.get_future();
    thread t1(job1, "test", move(j1_promise));
    j1_future.wait();  // wait for result
    cout << "result=" << j1_future.get() << '\n';
    t1.join();  // wait for thread completion

    // Demonstrate using promise<void> to signal state between threads.
    promise<void> j2_promise;
    future<void> j2_future = j2_promise.get_future();
    thread t2(job2, move(j2_promise));
    j2_future.wait();
    t2.join();
}

构建日志:

在/Users/maurits/Github/CPP-Demonstrations/src/Demonstration9.cpp:9中包含的文件中:/Users/maurits/Github/CPP-Demonstrations/src/Demonstration9.hpp:13中包含的文件:/ Applications / Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / thread:342:5:错误:尝试使用已删除的函数__invoke(VSTD :: move(VSTD :: get <1> (t)), VSTD :: move(VSTD :: get <Indices>( t))...); ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / thread:352:5:注意:在实例化函数模板特化'std :: __ 1 :: __ thread_execute>时, bool(*)(std :: __ 1 :: basic_string),const char *,std :: __ 1 :: promise,2,3>'在这里请求__thread_execute( p,Index()); ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / thread:368:47:注意:在实例化函数模板特化'std :: __ 1 :: __ thread_proxy>时, bool(*)(std :: __ 1 :: basic_string),const char *,std :: __ 1 :: promise >>>此处请求int ec = libcpp_thread_create(& t,& thread_proxy <Gp>, p.get()); ^ /Users/maurits/Github/CPP-Demonstrations/src/Demonstration9.cpp:26:12:注意:在实例化函数模板特化'std :: __ 1 :: thread :: thread)中,char const(&)[5 ],std :: __ 1 :: promise,void>'在这里请求线程t1(job1,“test”,move(j1_promise)); ^在/Users/maurits/Github/CPP-Demonstrations/src/Demonstration9.cpp:9中包含的文件中:/Users/maurits/Github/CPP-Demonstrations/src/Demonstration9.hpp:12中包含的文件:包含在文件中来自/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / iostream:38:包含在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain中的文件/ usr / include / c / v1 / ios:216:包含在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / __ locale中的文件:15:包含在文件中/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / string:470:包含在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/中的文件usr / include / c / v1 / string_view:169:包含在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / __ string中的文件:56:包含在文件中/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / algorithm:640:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/ c / v1 / type_traits:1590:5:注意:'~__nat'已在此明确标记删除~__nat()= delete; ^在/Users/maurits/Github/CPP-Demonstrations/src/Demonstration9.cpp:9中包含的文件中:/Users/maurits/Github/CPP-Demonstrations/src/Demonstration9.hpp:13中包含的文件:/ Applications / Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / thread:342:5:错误:尝试使用已删除的函数__invoke(VSTD :: move(VSTD :: get <1> (t)), VSTD :: move(VSTD :: get <Indices>( t))...); ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / thread:352:5:注意:在实例化函数模板特化'std :: __ 1 :: __ thread_execute>时, void(*)(),std :: __ 1 :: promise,2>'此处请求__thread_execute( p,Index()); ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / thread:368:47:注意:在实例化函数模板特化'std :: __ 1 :: __ thread_proxy>时, void(*)(),std :: __ 1 :: promise >>'此处请求int ec = libcpp_thread_create(& t,& thread_proxy <Gp>, p.get()); ^ /Users/maurits/Github/CPP-Demonstrations/src/Demonstration9.cpp:34:12:注意:在实例化函数模板特化'std :: __ 1 :: thread :: thread,void>'这里请求线程t2( job2,move(j2_promise)); ^在/Users/maurits/Github/CPP-Demonstrations/src/Demonstration9.cpp:9中包含的文件:包含在的文件中/Users/maurits/Github/CPP-Demonstrations/src/Demonstration9.hpp:12:在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / iostream中包含的文件中: 38:在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / ios:216中包含的文件中:/Applications/Xcode.app/Contents/Developer/中包含的文件工具链/ XcodeDefault.xctoolchain / usr / include / c / v1 / __ locale:15:包含在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / string:470中的文件:在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / string_view中包含的文件中:169:在/Applications/Xcode.app/Contents/Developer/Toolchains中包含的文件中/XcodeDefault.xctoolchain/usr/include/c / v1 / __ string:56:包含在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / algorithm:640中的文件:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c / v1 / type_traits:1590:5:注意:'~__nat'已在此明确标记删除~__nat()= delete;生成^ 2错误 .

1 回答

  • 1

    这条线

    thread t1(job1, "test", move(j1_promise));
    

    试图调用

    job1("test", move(j1_promise));
    

    在一个单独的线程中 . 但是你的 job1 只需要一个参数 .

相关问题