-2

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.

dandan78
  • 13,328
  • 13
  • 64
  • 78
puCCa
  • 1
  • 1
  • 2
  • 4
    You could just read the documentation: http://en.cppreference.com/w/cpp/numeric/complex . Also, what's with the pointers? What do you need them for? – Sebastian Redl Nov 20 '14 at 09:01
  • this is a sample code that is why I defined them as 20. normally it is a piece of huge program and there is 44100 values in pointer. Every other function use them as pointer so I am not allowed to change it to just array I have to use it as pointer. Before I asked my question I read documentation and tried solutions but did not work. I wanna know if there is a possibility that I have to write a header file with all that functions? – puCCa Nov 20 '14 at 09:14
  • 1
    Does the documentation for `` say that it defines a global `real` function? If so, where does it say so? If not, why do you think your code should work? –  Nov 20 '14 at 09:17
  • http://en.cppreference.com/w/c/numeric/complex/creal I used this file and in their sample they used it like how I use, I tried crealf creal normal real. And from the documentation you linked I am not able to understand easily. Am I supposed to define all this functions in my cpp file I am working currently on? or are there any complex.h header file available to download. Thanks! – puCCa Nov 20 '14 at 09:30
  • (To be clear, I didn't link to any documentation.) `` and `` are two different headers. You're linking to the C documentation for a function declared in ``, not to C++ documentation, even though your question is tagged C++. If your implementation doesn't have that, you generally can't just download it somewhere else, it needs to be made to work for your specific implementation. Since your implementation does not have ``, but does have ``, I'd say you should study the functionality provided by `` and use that. –  Nov 20 '14 at 09:41

2 Answers2

2
  1. complex is 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>.

  2. The type template complex and the functions real and imag are in the std namespace.

    Regarding complex<...>, you can either write std::complex<...> or put using std::complex; below your includes. (You could also writeusing namespace std;` but that might be dangerous to get used to it.)

    Regarding real and imag, they can use ADL (argument dependent lookup: when their argument is in the std namespace, the function name is automatically looked up in std too), so you don't need to specify the namespace for these functions.

  3. In the line a = &array1; (and the other one analogous), you point to the whole array array1, which is a pointer to array. What you probably want is either &array[1] or just array1, as arrays can be converted implicitly to the pointer to their first element.

  4. In *a[i] you access the i-th element in the array a points to (a itself 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.

leemes
  • 44,967
  • 21
  • 135
  • 183
-1

You probably want to use it like :

#include <complex>


int main()
{
    std::complex<float> *a;
    std::complex<float> *b;
    std::complex<float> array1[20];
    std::complex<float> array2[20];
    int real_part_array1[20];
    int real_part_array2[20];
    int imag_part_array1[20];
    int imag_part_array2[20];
    a = array1;
    b = array2;
    int i=0;
    for (i=0;i<=19;i++)
    {
    real_part_array1[i] = std::real(a[i]);
    imag_part_array1[i] = std::imag(a[i]);
    real_part_array2[i] = std::real(b[i]);
    imag_part_array2[i] = std::imag(b[i]); 
    }
    return 0;
}

complex is not a type but a type template. Hence you may want to use something like complex< int > or complex< double >.

Also real and imag are in the std namespace, hence std::real and std::imag.

Now when you say a = &array1, array1 is already a pointer, you are assigning a pointer to a pointer to the LHS, which is a pointer, this is a type error

jester
  • 3,491
  • 19
  • 30
  • `std::complex` has no meaning in standard C++. To quote the standard: "The effect of instantiating the template `complex` for any type other than `float`, `double`, or `long double` is unspecified." –  Nov 20 '14 at 09:44
  • Yes, sorry my bad. Totally missed that. Updated the answer. – jester Nov 20 '14 at 09:47