为什么函数中的std :: this_thread失败? [英] Why std::this_thread in function fails?

查看:452
本文介绍了为什么函数中的std :: this_thread失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个函数无法编译?

看起来很无辜。

我经常使用它并且工作正常,但每当我尝试使用它时,它失败了。

  #include   <   iostream  >  
#include < chrono >
#include < thread > ;
使用 命名空间 std :: literals;

void pauseScreen( float & duration){
< span class =code-keyword> if (duration == 0 5 ) {
std :: this_thread :: sleep_for( 0 .5s);
} else if (duration == 1 ){
std :: this_thread :: sleep_for(1s);
} else if (duration == 2 ){
std :: this_thread :: sleep_for(2s);
} else {
std :: this_thread :: sleep_for(4s);
}
}

int main(){
std :: cout<< Hello;
pauseScreen( 1 );
return 0 ;
}





更新:pauseScreen是我当前项目的确切功能(没有其他几千行代码)。注释掉这个函数可以让我的项目编译。在新的空白项目上测试时,它编译得很好。



我尝试过:



如果我删除该函数并放入

std :: this_thread :: sleep_for(1s);在hello消息的正下方,它编译。<​​div class =h2_lin>解决方案

你的代码中有几个缺陷:



  1. 您已经为基本类型而不是值参数声明了引用参数(您不打算对其进行修改 - 那么,为什么是引用参数?)。
  2. 您将右值传递给左值引用(文字到引用) - 这是不允许的。
  3. 您有一个浮点而不是double参数(浮点文字的类型是double,除非你添加一个float后缀(例如1.0F))。
  4. 你将一个int传递给一个浮点数(假设你用一个普通浮点数替换浮点引用) - 这不是一个错误,但你添加没有原因的复杂性。



  void  pauseScreen(  double  duration_in_sec ){
if duration_in_sec == 0 5 ){
std :: this_thread :: sleep_for( 0 。 5秒);
} else if duration_in_sec == < span class =code-digit> 1 。 0 ){
std :: this_thread :: sleep_for(1s) ;
} else if duration_in_sec == < span class =code-digit> 2 。 0 ){
std :: this_thread :: sleep_for(2s) ;
} else {
std :: this_thread :: sleep_for(4s);
}
}

int main(){
std :: cout<< Hello;
pauseScreen( 1 0 );
return 0 ;
}





干杯

Andi


Why this function fail to compile?
It look innocent enough.
I use it a lot and works fine, but whenever I try to make a function of it, it fails.

#include <iostream>
#include <chrono>
#include <thread>
using namespace std::literals;

void pauseScreen(float &duration) {
	if ( duration == 0.5 ) {
		std::this_thread::sleep_for(0.5s);
	} else if ( duration == 1 ) {
		std::this_thread::sleep_for(1s);
	} else if ( duration == 2 ) {
		std::this_thread::sleep_for(2s);
	} else {
		std::this_thread::sleep_for(4s);
	}
}

int main(){
    std::cout<<"Hello";
    pauseScreen(1);
    return 0;
}



Update: pauseScreen is the exact function from my current project (without other few thousands lines of code). Commenting out this function allow my project to compile. When tested on new blank project, it compiles fine.

What I have tried:

if I remove the function and put
std::this_thread::sleep_for(1s); directly below the the hello message, it compiles.

解决方案

You have several flaws in your code:


  1. You have declared a reference parameter for a fundamental type instead of a value parameter (you do not intend to modify it - so, why a reference parameter?).
  2. You pass an rvalue to an lvalue reference (a literal to a reference) - this is not allowed.
  3. You have a float instead of the double parameter (a floating-point literal is of type double unless you add a float suffix (e.g. 1.0F)).
  4. You pass an int to a float (assuming you replace the float reference by a plain float) - this is not an error, but you add complexity without cause.


How about

void pauseScreen(double duration_in_sec) {
	if ( duration_in_sec == 0.5 ) {
		std::this_thread::sleep_for(0.5s);
	} else if ( duration_in_sec == 1.0 ) {
		std::this_thread::sleep_for(1s);
	} else if ( duration_in_sec == 2.0 ) {
		std::this_thread::sleep_for(2s);
	} else {
		std::this_thread::sleep_for(4s);
	}
}
 
int main(){
    std::cout<<"Hello";
    pauseScreen(1.0);
    return 0;
}



Cheers
Andi


这篇关于为什么函数中的std :: this_thread失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆