Problems with header files and complex numbers

Hi, I need to implement a numerical algorithm using complex numbers. But, I haven't get that working yet, because I have problems with header files. (I haven't used those before) I have tried, for instance, something like this http://www.cplusplus.com/forum/general/23930/ but still I couldn't get my code working. Basically, what the code should do is to evaluate (eval) complex function (fun) at point z. Finally it should return a complex number. Here is the code

//main.cpp
#include <iostream>
#include <complex>
#include "cplxOp.h"
using namespace std;
typedef complex<double> cplx;
typedef cplx (*function)(cplx );
cplx fun(cplx z){
return cplx(2,0)*z;
}
int main(){
ComplexOper Operation;
Operation.z = cplx(1,1);
Operation.fun = fun;
cout << "Complex number = " << Operation.eval << endl;
return 0;
}

//cplxOp.h
#ifndef CPLXOP_H
#define CPLXOP_H
#include <complex>
typedef cplx<double> cplx;
typedef cplx (*function)(cplx);
ComplexOper{
public:
cplx z;
function fun;
cplx eval(function fun,cplx z);
};
#endif

//cplxOp.cpp
#include <complex>
#include "cplxOp.h"
using namespace std;
cplx eval(function fun, cplx z){
return fun(z);
}

Errors:
cplxOp.h:4:9: error: ‘cplx’ does not name a type
cplxOp.h:5:14: error: ISO C++ forbids declaration of ‘cplx’ with no type [-fpermissive]
cplxOp.h:5:14: error: typedef ‘cplx’ is initialized (use decltype instead)
cplxOp.h:5:16: error: ‘function’ was not declared in this scope
cplxOp.h:6:1: error: ‘ComplexOper’ does not name a type
Last edited on
The first error is pretty self-explanitory:

1
2
3
//cplxOp.h
...
typedef cplx<double> cplx; // <-  


cplx is not defined at this point, so you can't typedef cplx and cplx. You probably meant to do this: (complex, not cplx)

typedef complex<double> cplx;

The 3 errors that follow seem to be caused by that problem as well.

The last error:

1
2
ComplexOper{
public:


ComplexOper isn't defined. If you are trying to make a class, you need to tell the compiler you want it to be a class with the 'class' keyword:

class ComplexOper{



Also, if you are typdefing cplx and function in cplxOp.h, there is no need to do it again in main.cpp.


Lastly, this does not do what you expect:

cout << "Complex number = " << Operation.eval << endl;

eval is a function. If you want to call that function, you need to use the parenthesis and give it a parameter list:

Operation.eval( __X__, __Y__ )


Without that, you are just getting a pointer to the function, which will print an address and not an actual complex number like you want.
Sorry, I didn't read my code properly. But I still get a error. The code:

//main
#include <iostream>
#include <complex>
#include "cplxOp.h"
using namespace std;
cplx fun(cplx z){
return cplx(2,0)*z;
}
int main(){
ComplexOper Operation;
cplx z = cplx(1,1);
cout << "Cplx number = "<< Operation.eval(fun,z) << endl;
return 0;
}

//cplxOp.h
#ifndef CPLXOP_H
#define CPLXOP_H
#include <complex>
using namespace std;
typedef complex<double> cplx;
typedef cplx (*function)(cplx);
class ComplexOper{
public:
cplx eval(function fun,cplx z);
};
#endif

//cplxOp.cpp
#include <complex>
#include "cplxOp.h"
using namespace std;
cplx eval(function fun, cplx z){
return fun(z);
}

Compiler still says that:
/tmp/ccBzQCgQ.o: In function `main':
main.cpp:(.text+0x90): undefined reference to `ComplexOper::eval(std::complex<double> (*)(std::complex<double>), std::complex<double>)'
collect2: ld returned 1 exit status

and I simply compile the code
g++ main.cpp cplxOp.cpp
or
g++ -c main.cpp
g++ -c cplxOp.cpp
g++ main.o cplx.o -o main
Last edited on
Your first error is
compiler wrote:
cplxOp.h:4:9: error: ‘complex’ does not name a type

because there is no type called "complex".

To fix, replace
typedef complex<double> cplx;
with
typedef std::complex<double> cplx;

You will get no more compiler errors, but a linker error:

linker wrote:
undefined reference to `ComplexOper::eval(std::complex<double> (*)(std::complex<double>), std::complex<double>)'

Because the member function eval of class ComplexOper was declared but never defined.
To fix, replace
cplx eval(function fun, cplx z){
with
cplx ComplexOper::eval(function fun, cplx z){

Thank you for both of you. Problem solved and my code is working.
Topic archived. No new replies allowed.