Problem when setting precision with cout: Candidates are: ? precision() ? precision(?)

I want to set a specific precision in a float number. So I found the public member function "std::ios_base::precision" and did a small exercise that worked correctly:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main() {
	double f=3.14159;
	cout.precision(10);
	cout.setf(ios::fixed, ios::floatfield);
	cout<<f<<endl; //cout displays 10 numbers after the comma
	return 0;
}


Now I want to use this feature in my exercise:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <chrono>
#include <thread>

using namespace std;

//Prototypes
void f();

int main() {
	clock_t c_start=clock();
	auto t_start=chrono::high_resolution_clock::now();
	thread t1(f);
	thread t2(f);
	t1.join();
	t2.join();
	clock_t c_end=clock();
	auto t_end=chrono::high_resolution_clock::now();

	//cout.unsetf(ios::floatfield);
	cout.precision(2);   //The compiler complains at this line
	cout.setf(ios::fixed, ios::floatfield);
	cout<<"CPU time used"<< 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " ms\n";



	return 0;
}

void f(){
	volatile double d=0;
	for(int n=0;n<10000;n++){
		for(int m=0; m<10000; m++){
			d += d*n*m;
		}
	}
}


The compiler complains at
cout.precision(2);
with the following error:
Candidates are: ? precision() ? precision(?)

Does anybody know why I receive this error and how can I solve it?
Thanks.
Last edited on
Everything you did is correct, and it compiled just fine to me, but i had to first set the flag of
-std=c++11, because one of your libraries requires so, Other than that, it compiled just fine for me, i don't know what issue you have there.
I also set the flag "-std=c++11" but I still have the error. @masecla33 It is strange that for you is working. I wonder what it can be wrong with my compilation...
Perhaps you need to #include <iomanip> which contains the definitions of the io manipulator functions.



Here is the exact code i have been using.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <chrono>
#include <thread>
#include <iomanip> //for me it compiled just fine without this tough i added just to be sure

using namespace std;

//Prototypes
void f();

int main() {
	clock_t c_start=clock();
	auto t_start=chrono::high_resolution_clock::now();
	thread t1(f);
	thread t2(f);
	t1.join();
	t2.join();
	clock_t c_end=clock();
	auto t_end=chrono::high_resolution_clock::now();

	//cout.unsetf(ios::floatfield);
	cout.precision(2);   //The compiler complains at this line
	cout.setf(ios::fixed, ios::floatfield);
	cout<<"CPU time used"<< 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " ms\n";



	return 0;
}

void f(){
	volatile double d=0;
	for(int n=0;n<10000;n++){
		for(int m=0; m<10000; m++){
			d += d*n*m;
		}
	}
}

Try making another project with the same code and stuff.
Last edited on
Topic archived. No new replies allowed.