Problems with #include<complex.h>

  • Context:
  • Thread starter Thread starter JorgeM
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on the implementation of the complex.h library in C++ for complex number calculations. The user encountered compilation errors due to incorrect syntax and mixing C and C++ conventions. The correct approach in C++ is to use complex instead of double complex, and to utilize real() and imag() functions for accessing real and imaginary parts. The provided examples demonstrate the correct usage in both C and C++ environments.

PREREQUISITES
  • Understanding of C++ syntax and conventions
  • Familiarity with the complex.h library
  • Knowledge of complex number representation in programming
  • Experience with basic input/output operations in C++
NEXT STEPS
  • Learn about std::complex in C++ and its methods
  • Explore the differences between C and C++ libraries for complex numbers
  • Investigate error handling in C++ compilation
  • Practice implementing complex number calculations using std::complex
USEFUL FOR

C++ developers, students learning about complex numbers, and programmers transitioning from C to C++ who need to understand the correct usage of the complex.h library.

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);
    count<<" 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   Reactions: 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);
count<<" z = "<<real(z)<<" + " <<imag(z)<<" i"<<endl;
return 0;
}
 
  • Like
Likes   Reactions: jim mcnamara, FactChecker, JorgeM and 1 other person

Similar threads

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