Problems with #include<complex.h>

  • C/++/#
  • Thread starter JorgeM
  • Start date
  • #1
26
6

Main Question or Discussion Point

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
34,056
9,918
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
 
  • #3
phyzguy
Science Advisor
4,396
1,375
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;
}
 

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

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