【发布时间】:2018-10-16 17:13:15
【问题描述】:
我正在尝试编写一个包含承诺/未来的示例。在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(__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), 字符 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() = 删除; ^ 在 /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(__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/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() = 删除; ^ 生成 2 个错误。
【问题讨论】:
-
您应该发布构建日志、
DemonstrationInterface.hpp内容和调用Run()的代码。并且可能简化为 mcve。 -
我删除了接口。这段代码没有意义。我还添加了日志。感谢您的反馈!
-
您的函数签名不匹配。
job1示例在您尝试传递两个参数时仅接受一个参数 - 字符串和承诺。 -
错误信息可能是一条红鲱鱼。研究您链接的页面上的函数原型,并将它们与您的进行比较。
-
犯了这样一个愚蠢的错误。谢谢大家!
标签: c++ xcode promise move future