TBB를 사용하는 방법을 배우려고합니다. 따라서 복소수 배열의 거듭 제곱을 계산하도록 설계된 샘플 프로그램을 수정하고 있습니다. 원래는 parallel_for 루프에 배열을 전달하고 있었지만 벡터로 전달되도록 배열을 변경하려고합니다. 그러나, 나는 내 코드를 컴파일 할 수 없다. 나는 (사용 g ++ -g program_name.cpp -ltbb 컴파일) 다음과 같은 오류가 발생합니다 :C++ parallel_for 오류
error: passing ‘const std::complex<double>’ as ‘this’ argument
discards qualifiers [-fpermissive] result[i] = z;
내 코드는 다음과 같습니다 : 당신은에 비 mutable
멤버 필드 result
을 수정하려는
#include <cstdlib>
#include <cmath>
#include <complex>
#include <ctime>
#include <iostream>
#include <iomanip>
#include "tbb/tbb.h"
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include "tbb/parallel_for_each.h"
#include "tbb/task_scheduler_init.h"
using namespace std;
using namespace tbb;
typedef complex<double> dcmplx;
dcmplx random_dcmplx (void)
{
double e = 2*M_PI*((double) rand())/RAND_MAX;
dcmplx c(cos(e),sin(e));
return c;
}
class ComputePowers
{
vector<dcmplx> c; // numbers on input
int d; // degree
vector<dcmplx> result; // output
public:
ComputePowers(vector<dcmplx> x, int deg, vector<dcmplx> y): c(x), d(deg), result(y) { }
void operator() (const blocked_range<size_t>& r) const
{
for(int i=r.begin(); i!=r.end(); ++i)
{
dcmplx z(1.0,0.0);
for(int j=0; j < d; j++) {
z = z*c[i];
};
result[i] = z;
}
}
};
int main (int argc, char *argv[])
{
int deg = 100;
int dim = 10;
vector<dcmplx> r;
for(int i=0; i<dim; i++)
r.push_back(random_dcmplx());
vector<dcmplx> s(dim);
task_scheduler_init init(task_scheduler_init::automatic);
parallel_for(blocked_range<size_t>(0,dim),
ComputePowers(r,deg,s));
for(int i=0; i<dim; i++)
cout << scientific << setprecision(4)
<< "x[" << i << "] = (" << s[i].real()
<< " , " << s[i].imag() << ")\n";
return 0;
}
이 경우, 당신은 여전히'연산자 오류를 이해하지 못했다()'방법을 당신이하고있는'결과 [I] = Z;'. 이 메소드는'const'로 표시되며'const'로 표시된 메소드에서 데이터 멤버를 수정하지 않아야합니다. – Jagannath