Problems with #include<complex.h>

  • C/++/#
  • Thread starter JorgeM
  • Start date
  • #1
30
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:

Answers and Replies

  • #2
35,348
11,680
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
  • #3
phyzguy
Science Advisor
4,778
1,733
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

Related Threads on Problems with #include<complex.h>

Replies
3
Views
9K
  • Last Post
Replies
5
Views
13K
Replies
2
Views
695
  • Last Post
Replies
5
Views
635
Replies
4
Views
5K
  • Last Post
Replies
20
Views
10K
Replies
7
Views
2K
Replies
4
Views
1K
  • Last Post
Replies
3
Views
2K
Replies
17
Views
3K
Top