线程构造函数通过std::tuple'产生MSVC C2661编译器错误;s构造函数 根据Kelvin Henney的功能C++,我在YouTube上进行了一次演讲。在视频播放大约50分钟后,他开始展示一个他命名为channel的示例类结构。然后,他编写了简单的fizzbuzz函数,并将使用线程将其传递到一段类似服务器的代码中。我正在使用他的视频中的代码,可以在这里找到:
但是,当我尝试编译程序时,Visual Studio正在生成指向线程构造函数通过std::tuple'产生MSVC C2661编译器错误;s构造函数 根据Kelvin Henney的功能C++,我在YouTube上进行了一次演讲。在视频播放大约50分钟后,他开始展示一个他命名为channel的示例类结构。然后,他编写了简单的fizzbuzz函数,并将使用线程将其传递到一段类似服务器的代码中。我正在使用他的视频中的代码,可以在这里找到:,c++,compiler-errors,visual-studio-2017,c++17,stdthread,C++,Compiler Errors,Visual Studio 2017,C++17,Stdthread,但是,当我尝试编译程序时,Visual Studio正在生成指向std::tuple的C2661编译器错误。。。它来自我的代码中的std::tread构造函数 main.cpp #include <iostream> #include <exception> #include <string> #include <thread> #include "server.h" std::string fizzbuzz(int n)
std::tuple
的C2661
编译器错误。。。它来自我的代码中的std::tread
构造函数
main.cpp
#include <iostream>
#include <exception>
#include <string>
#include <thread>
#include "server.h"
std::string fizzbuzz(int n) {
return
n % 15 == 0 ? "FizzBuzz" :
n % 3 == 0 ? "Fizz" :
n % 5 == 0 ? "Buzz" :
std::to_string(n);
}
void fizzbuzzer(channel<int> & in, channel<std::string> & out) {
for (;;)
{
int n;
in.receive(n);
out.send(fizzbuzz(n));
}
}
int main() {
try {
channel<int> out;
channel<std::string> back;
std::thread fizzbuzzing(fizzbuzzer, out, back);
for (int n = 1; n <= 100; ++n) {
out << n;
std::string result;
back >> result;
std::cout << result << "\n";
}
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
#包括“server.h”
标准::字符串嘶嘶作响(int n){
返回
n%15==0?“嘶嘶作响”:
n%3==0?“嘶嘶作响”:
n%5==0?“嗡嗡声”:
std::to_字符串(n);
}
无效泡沫蜂鸣器(通道和输入、通道和输出){
对于(;;)
{
int n;
in.接收(n);
发送(fizzbuzz(n));
}
}
int main(){
试一试{
通道输出;
沟回;
螺纹嘶嘶作响(嘶嘶作响、向外、向后);
对于(int n=1;n结果;
同样,如果我将其更改为针对x64构建,它仍然会给出相同的编译器错误…std::thread fizzbuzzing(fizzbuzzer,std::ref(out),std::ref(back))
您需要在线程超出作用域之前加入线程,这可能会在您想等待线程完成执行时修复中止。您的问题解决了吗?如果没有,我认为这可能会对您有所帮助。此外,如果我将其更改为build for x64,仍然会出现相同的编译器错误…std::thread fizzbuzzing(fizzbuzzer,std::ref(out),std::ref(back));
您需要在线程超出范围之前加入线程,这可能会修复中止。之后,当您希望等待线程完成执行时,您的问题得到解决了吗?如果没有,我想这可能会对您有所帮助。
#pragma once
#include <condition_variable>
#include <queue>
#include <mutex>
#include <iostream>
template<typename ValueType>
class receiving;
template<typename ValueType>
class channel {
private:
std::mutex key;
std::condition_variable_any non_empty;
std::queue<ValueType> fifo;
public:
void send(const ValueType & to_send) {
std::lock_guard<std::mutex> guard(key);
fifo.push(to_send);
non_empty.notify_all();
}
bool try_receive(ValueType & to_receive) {
bool received = false;
if (key.try_lock()) {
std::lock_guard<std::mutex> guard(key, std::adopt_lock);
if (!fifo.empty()) {
to_receive = fifo.front();
fifo.pop();
received = true;
}
}
return received;
}
void receive(ValueType & to_receive) {
std::lock_guard<std::mutex> guard(key);
non_empty.wait(
key,
[this] {
return !fifo.empty();
});
to_receive = fifo.front();
fifo.pop();
}
void operator<<(const ValueType & to_send) {
send(to_send);
}
receiving<ValueType> operator>>(ValueType & to_receive) {
return receiving(this, to_receive);
}
};
template<typename ValueType>
class receiving {
private:
channel<ValueType> * that;
ValueType & to_receive;
public:
receiving(channel<ValueType> * that, ValueType & to_receive)
: that(that), to_receive(to_receive)
{}
receiving(receiving && other)
: that(other.that), to_receive(other.to_receive)
{
other.that = nullptr;
}
operator bool() {
auto from = that;
that = nullptr;
return from && from->try_recieve(to_receive);
}
~receiving() {
if (that)
that->receive(to_receive);
}
};
1>------ Build started: Project: Computations, Configuration: Debug Win32 ------
1>main.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\memory(2539): error C2661: 'std::tuple<void (__cdecl *)(channel<int> &,channel<std::string> &),channel<int>,channel<std::string>>::tuple': no overloaded function takes 3 arguments
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\thread(46): note: see reference to function template instantiation 'std::unique_ptr<std::tuple<void (__cdecl *)(channel<int> &,channel<std::string> &),channel<int>,channel<std::string>>,std::default_delete<_Ty>> std::make_unique<std::tuple<void (__cdecl *)(channel<int> &,channel<std::string> &),channel<int>,channel<std::string>>,void(__cdecl &)(channel<int> &,channel<std::string> &),channel<int>&,channel<std::string>&,0>(void (__cdecl &)(channel<int> &,channel<std::string> &),channel<int> &,channel<std::string> &)' being compiled
1> with
1> [
1> _Ty=std::tuple<void (__cdecl *)(channel<int> &,channel<std::string> &),channel<int>,channel<std::string>>
1> ]
1>c:\users\skilz99\source\repos\computations\computations\main.cpp(31): note: see reference to function template instantiation 'std::thread::thread<void(__cdecl &)(channel<int> &,channel<std::string> &),channel<int>&,channel<std::string>&,void>(_Fn,channel<int> &,channel<std::string> &)' being compiled
1> with
1> [
1> _Fn=void (__cdecl &)(channel<int> &,channel<std::string> &)
1> ]
1>Done building project "Computations.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========