Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C++
#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.
Posted
Updated 18-Feb-16 11:57am
v2
Comments
Andreas Gieriet 17-Feb-16 15:43pm    
What is the error message?
Cheers
Andi
Are Riff 17-Feb-16 16:21pm    
Binary '+=': no operator found which takes a right-hand operandi of type 'const std::chrono::duration<double,std::ratio<1,1>>'(or there is no acceptable conversion)

1 solution

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
C++
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
 
Share this answer
 
v2
Comments
CPallini 17-Feb-16 16:07pm    
5.
Andreas Gieriet 17-Feb-16 16:16pm    
Thanks for your 5!
Cheers
Andi
Are Riff 17-Feb-16 16:25pm    
Nope still doesn't work. Besides double, I tried unsigned long long. Same. Same error. Binary '+=': no operator found which takes a right-hand operandi of type 'const std::chrono::duration<double,std::ratio<1,1>>'(or there is no acceptable conversion)
Andreas Gieriet 17-Feb-16 16:36pm    
Have you removed the reference?
On which line is this error reported?
What if you rename the parameter name duration to something like duration_in_sec?
Andi
Are Riff 17-Feb-16 19:05pm    
I've removed the reference. I also tried it with const reference.
The error were reported on the .obj files, not on any lines.
But using the g++ it can compile fine. weird.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900