I write a code in order to write prime number in a file, and after that I write another function in order to part the work in many threading. But my problem is the compiler write me an error
error C2664: 'void (int,int,std::ofstream &)' : cannot convert argument 3 from 'std::basic_string,std::allocator>' to 'std::ofstream &'.
But my problem is that I don't understand what the error says and where I do a thing that is forbidden.
This is my code:
void writePrimesToFile(int begin, int end, ofstream& file)
{
int i, j, prime = 1;
for (i = begin; i <= end; i++) {
for (j = begin; i <= end / 2; j++) {
if (i % j == 0) {
prime = 0;
} else {
file << i << endl;
}
}
}
}
void callWritePrimesMultipleThreads(int begin, int end, string filePath, int N)
{
double startTimer, stopTimer;
startTimer = clock();
thread* arr = new thread[N];
for (int i = 0; i < N; i++) {
int start = begin;
int finish = N;
arr[i] = thread(writePrimesToFile, start, finish, ref(filePath));
start = finish;
finish += N;
}
for (int i = 0; i < N; i++) {
arr[i].join();
}
stopTimer = clock();
cout << "The time that takes is: " <<
double(stopTimer - startTimer) / CLOCKS_PER_SEC << endl;
}
std::stringobject to your thread function, which wants astd::ofstreamreference (clearly stated in the message). That will not work very well. - Some programmer dudestd::vectorinstead? - Some programmer dudethread* arr = new thread[N];do thisstd::vector<std::thread> arr;followed byarr.emplace_back(writePrimesToFile, start, finish... etc);- Galik&&when I could have just used&out of habit (it works in more situations). Sofor(auto& thread: arr)basically iterates through all the threads in the vector giving you a reference to each one that you can use tojoin(). It is like saying for each thread in arrjoin()that thread. - Galik