Question

I would like to bring forward an issue regarding MinGW 4.7.2 I first ran into the deadly issue caused by libstdc++-6.dll when I ventured in OpenCV. Luckily, I ran across a workaround here -> http://answers.opencv.org/question/3740/opencv-243-mingw-cannot-run-program/. It looked great, for a time.

Now I am trying to implement complex numbers. I try the following code

#include <iostream>
#include <complex.h>
using namespace std;

int main(int argc, char* argv[])
{
    float _Complex d = 2.0f + 2.0f*I;
    cout << "Testing Complex\t" << d;
    return 0;
}

It should run, by all means. I face no errors or warnings while linking. I use CodeBlocks as my preferred IDE in Windows. But, again, I am stymied. Here is the AppCrash report

Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: complex.exe
  Application Version:  0.0.0.0
  Application Timestamp:    515d61f7
  Fault Module Name:    libstdc++-6.dll
  Fault Module Version: 0.0.0.0
  Fault Module Timestamp:   4ed82a4d
  Exception Code:   c0000005
  Exception Offset: 000462bc
  OS Version:   6.1.7600.2.0.0.256.1
  Locale ID:    1033
  Additional Information 1: 0a9e
  Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
  Additional Information 3: 0a9e
  Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

Yet amazingly, How to work with complex numbers in C? provides a perfectly working C code!

#include <stdio.h>      /* Standard Library of Input and Output */
#include <complex.h>    /* Standart Library of Complex Numbers */

int main()
{
    double complex z1 = 1.0 + 3.0 * I;
    double complex z2 = 1.0 - 4.0 * I;

    printf("Working with complex numbers:\n");
    printf("Starting values: Z1 = %.2f  + %.2fi\tZ2 = %.2f  + %.2fi\n",creal(z1),cimag(z1),creal(z2),cimag(z2));
    double complex sum = z1 + z2;
    printf("The sum: Z1 + Z2 = %.2f %+.2fi\n", creal(sum), cimag(sum));
    return 0;
}

As you can see, again the faulty libstdc++-6.dll comes into play. Can anyone suggest me any workaround this time, hopefully without downgrading to previous versions of MinGW, as I would then have to rebuild all my libraries.

Any help will be appreciated!

Was it helpful?

Solution

Like Jens said I don't think the complex.h header file is compatible with C++. In C++ you should be using the complex header like so:

#include <iostream>
#include <complex>

int main()
{
  std::complex<double> c1(1.0,1.0), c2 ;

  c2 = pow(c1,2.0);

  std::cout << c1 << "  " << c2 << std::endl; 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top