Problems with #include<complex.h>

  • Thread starter Thread starter JorgeM
  • Start date Start date
Click For Summary
The discussion highlights issues with using the complex.h library in C++ for complex number calculations. The original code attempts to declare a complex number using C syntax, which leads to compilation errors. It clarifies that in C++, the correct approach is to use the complex class from the standard library, specifically `complex<double>`. Examples are provided to illustrate the correct syntax for both C and C++. Understanding the differences between C and C++ usage of complex numbers is essential for successful implementation.
JorgeM
Messages
30
Reaction score
6
Hi there. I have been trying to implement complex.h library to make some calculations in c++.Anyways I am not sure why mi compiler does not run my code at all.
C:
#include <iostream>
#include <complex.h>
#include <stdio.h>
using namespace std;
int main()
{
    double complex z = CMPLX(0.0, -0.0);
    cout<<" z = "<<creal(z)<<" + " <<cimag(z)<<endl;
    return 0;
}
Codeblocks says:
Error: expected initializer before z
Error: z was not declared in this scope
I am really confused because even in cpp.com this procedure for imaginary numbers is given.

If you could help me with how may I use, I would be really grateful.
Thanks a lot
 
Last edited by a moderator:
Technology news on Phys.org
JorgeM said:
double complex z
I don't know where you got that from but that cannot work. You are trying to assign a double called "complex", and then the compiler has no idea what to do with the z.
Here are examples how to do it
complex<double> z
 
  • Like
Likes FactChecker
I think you are mixing C and C++ usage. The following compiles and runs in C:

C:
#include <complex.h>
#include <stdio.h>

int main()
{
double complex z = CMPLX(0.0, -0.0);
printf("z = %.1f%+.1fi\n", creal(z), cimag(z));
return 0;
}

The following code compiles and runs in C++:

Code:
#include <iostream>
#include <complex.h>
#include <stdio.h>
using namespace std;
int main()
{
complex<double> z(0.0, 0.0);
cout<<" z = "<<real(z)<<" + " <<imag(z)<<" i"<<endl;
return 0;
}
 
  • Like
Likes jim mcnamara, FactChecker, JorgeM and 1 other person
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
3
Views
1K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K