- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试用 C++ 实现一个线程,当我拍照时它将用于控制,但是这个简单的代码给了我一个奇怪的错误,我无法理解。这是我的代码:
#include <iostream>
#include <fstream>
#include <thread>
using namespace std;
void counter(int seconds, bool &flagTakePhoto, bool &flagThreadStart);
int main() {
bool takePhoto, threadStart;
int seconds = 1;
thread t(counter, seconds, takePhoto, threadStart);
//Some code here
}
void counter(int seconds, bool &flagTakePhoto, bool &flagThreadStart) {
while (flagThreadStart) {
this_thread::sleep_for(chrono::seconds(seconds));
flagTakePhoto = true;
}
terminate();
}
这是错误:
1>------ Build started: Project: Proyecto para pruebas OPENCV, Configuration:
Release x64 ------
1> Main.cpp
1>Main.cpp(46): warning C4244: 'initializing': conversion from '__int64' to 'int', possible loss of data
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): note: With the following template arguments:
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): note: '_Callable=void (__cdecl *)(int,bool &,bool &)'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): note: '_Types={int, bool, bool}'
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0x00,0x01,0x02,0x03>(std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool> &,std::integer_sequence<_Ty,0x00,0x01,0x02,0x03>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>,
1> _Ty=size_t
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0x00,0x01,0x02,0x03>(std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool> &,std::integer_sequence<_Ty,0x00,0x01,0x02,0x03>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>,
1> _Ty=size_t
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(242): note: while compiling class template member function 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept'
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(230): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(256): note: see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thread(52): note: see reference to function template instantiation 'void std::_Launch<std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>>(_Thrd_t *,_Target &&)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1> ]
1> Main.cpp(136): note: see reference to function template instantiation 'std::thread::thread<void(__cdecl &)(int,bool &,bool &),int&,bool&,bool&,void>(_Fn,int &,bool &,bool &)' being compiled
1> with
1> [
1> _Fn=void (__cdecl &)(int,bool &,bool &)
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
更奇怪的是我在其他程序中也有类似的代码并且编译没有错误!有谁知道这段代码有什么问题?谢谢!
最佳答案
std::thread
constructor将构造它的所有对象并调用函数,就好像通过:
template <class T>
typename decay<T>::type decay_copy(T&& v) {
return std::forward<T>(v);
}
这使得参数类型为
thread t(counter, seconds, takePhoto, threadStart);
是int
, bool
, bool
.因为你不能调用 counter(int, bool, bool)
(它采用左值引用),该构造函数格式错误。
为了传递引用,您需要将参数包装在 std::reference_wrapper<T>
中.该类型可隐式转换为 T&
, 所以调用 counter(int, std::reference_wrapper<bool>, std::reference_wrapper<bool>)
已验证。对于简写,标准提供 std::ref
,因此您只需执行以下操作:
thread t(counter, seconds, std::ref(takePhoto), std::ref(threadStart));
关于c++ - 无法在线程中专门化函数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32765434/
我有一个 PowerBI Online 数据集,它是在 PowerBI 桌面中创建然后在线发布的。到目前为止,一切都很好。 我通过 PowerBI pusblish 从 Excel 连接到数据集,按预
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 2 年前。
我必须对一些太大而无法放入内存的数据训练分类模型,我正在使用 scikit learn 和 pandas 来进行分析。所以这是我的问题,如何在在线学习管道中使用验证来调整超参数? 我使用带有chuck
我正在开发一个应用程序,该应用程序将从 webservice 获取数据和图像并将其存储在设备中以供离线使用。同时,应用程序会将一些数据存储在 sqlite db 中,并将一些图像作为默认数据。 这是应
是否可以使用 FileReader API 和 onprogress 事件访问随 HTML5 传入的数据? 如果是这样,是否有 MD5 或其他快速散列算法的“在线”版本,以便我可以在文件完全读取之前开
希望任何人都可以帮助我更改下面的代码,我的临时文件包含以下代码: Temp=8.4* Humidity=70.4% 代替代码 Temp = 24 *C, Hum = 40 % 适用于以下脚本。 我需
我必须创建一个功能类似于联系人应用程序的应用程序。您可以在客户的 iPhone 上添加一个联系人,它应该会上传到客户的 iPad 上。如果客户在他们的 iPad 上更新联系人,它应该会在他们的 iPh
在 gitlab.com 上审查 merge 请求时,有时我必须在完成 merge 之前进行 rebase。 在 gitlab 上按“Rebase”后,我有一个特定的管道步骤失败,因为它无法验证用户的
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
我正在尝试在 azure 上托管 SQL 服务器以与节点应用程序进行通信。我已经成功地完成了创建数据库服务器和数据库本身的过程。现在,我想编辑我的数据库结构。据我发现online ,应该有一种方法可以
我在 Quickbooks Intuit 开发人员 API 中使用 Oauth 2 获得了访问 token 。 范围是 com.intuit.quickbooks.accounting 我能够使用 Q
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
是否可以使Angular Material progress spinner与文本并大致与字符的大小一致地显示? 我想要类似的东西: please wait 微调器仅与“请稍候”文本成行出现。 这可
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一个每天运行的Powershell脚本。今天它失败了,因为我正在使用的域 Controller 不可用。在继续执行脚本的其余部分之前,我想确保可以连接到可用的DC。 $LdapServer = "
我想制作一款在线 Flash 游戏,它将具有社交功能,但游戏玩法将主要是单人游戏。例如,屏幕上不会同时出现两个玩家,社交互动将通过异步消息进行,不会有实时聊天或其他任何内容。大部分逻辑将发生在客户端中
这几天我开始在线玩OpenShift。我部署了一个非常简单的“Hello World”Java 示例(1 行代码!),没有任何依赖项(没有 Spring!)命令行是这样的: oc.exe new-a
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
所以我一直在网上学习Java(初学者),并且我一直在尝试制作一个用于制作矩形的类文件。但是,我的在线 java 评估器指出它找不到实例变量。 This is the comment on it.我的代
我是一名优秀的程序员,十分优秀!