I have 2 pointers which points to two 20 member arrays. My arrays contains complex numbers. I want to make element by element division for that complex numbers that is why I need to separate numbers to real and imaginary parts. I tried the following code but it gives error.
#include <complex>
complex *a;
complex *b;
complex array1[20];
complex array2[20];
a = &array1;
b = &array2;
int i=0;
for (i=0;i<=19;i++)
{
real_part_array1[i] = real(*a[i]);
imag_part_array1[i] = imag(*a[i]);
real_part_array2[i] = real(*b[i]);
imag_part_array2[i] = imag(*b[i]);
}
First error I got was; I tried to write it as
#include <complex.h>
the error message was "cannot open source file complex.h". Then i deleted h and error was gone. The second error I have is for real() and imag(). The error message is "identifier real is undefined".
For division I have to seperate them to real and imaginary parts but I dont know how to solve that problem. I hope you guys can help me.
complexis not a type, it's a type template. You need to specify the type of the real and imaginary components as a template parameter, e.g.complex<double>.The type template
complexand the functionsrealandimagare in thestdnamespace.Regarding
complex<...>, you can either writestd::complex<...>or putusing std::complex; below your includes. (You could also writeusing namespace std;` but that might be dangerous to get used to it.)Regarding
realandimag, they can use ADL (argument dependent lookup: when their argument is in thestdnamespace, the function name is automatically looked up instdtoo), so you don't need to specify the namespace for these functions.In the line
a = &array1;(and the other one analogous), you point to the whole arrayarray1, which is a pointer to array. What you probably want is either&array[1]or justarray1, as arrays can be converted implicitly to the pointer to their first element.In
*a[i]you access the i-th element in the arrayapoints to (aitself is not a pointer but the array subscript operator works on pointers as if they were arrays). Then you dereference that complex type, which is invalid. Simply drop the*.You can see the final code here.