素数をファイルに書き込むためにコードを書きます。その後、多くのスレッドで作業を分けるために別の関数を書きます。しかし、私の問題は、コンパイラはエラーにofstream:ofstreamに変換できません&
error C2664: 'void (int,int,std::ofstream &)' : cannot convert argument 3 from 'std::basic_string,std::allocator>' to 'std::ofstream &'.
私を書きですが、私の問題は、私はエラーが言うことを理解していないと私は禁止されていることを行う場所ということです。
これは私のコードです:
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を望んでいるあなたのスレッド関数に'のstd :: STRING'オブジェクトへの参照を渡している:: ofstream'参照を(明確に述べ:簡単にし、代わりに
std::vector<std::thread>
を使用してきれいになりますメッセージに)。それはうまく動作しません。 –ああ、あなたにはメモリリークがあります。代わりに 'std :: vector'を使わないのはなぜですか? –
@Someprogrammerdudeどこでメモリリークがありますか?もっと説明できますか? –