I have written a piece of code that uses complex library. I have put the definition of my function in a header file and in the main.cpp i have defined the complex number “I” as you can see in my codes below.
But when i want to compile this code i receive errors.
I think the function in the header file can not use complex library.
How can i fix this problem?
Thanks.
main.cpp
#include <iostream>
#include "math.h"
#include <complex>
#include "header.h"
using namespace std;
typedef complex<double> cmp;
cmp I(0.0,1.0);
int main()
{
cout << function(5.0) << endl;
return 0;
}
header.h
#ifndef header
#define header
double function(double x)
{
return 5*exp(I*x).real();
}
#endif
The problem is that
Iis not defined when your header file is parsed. You would need to move the definition ofIbefore the#include "header.h".