How to use complex numbers in C++?

C++ have native suport for integer and real numbers, but not for complex numbers. All operations must be implemented or we should use the library <complex>.
I'm having problem in pass a complex number, as complex number and not to real numbers, into a class.
When I want to pass a complex number to a function, using the header <complex>, I do:



#include <iostream>
#include <complex>

using namespace std;

typedef complex<double> dcomp;

dcomp sum(dcomp x, dcomp y);

main()
{
dcomp x(2.0,3.0), y(1.0,2.);

cout << "soma de complexos = "<< sum(x,y) << endl;
}

dcomp sum(dcomp x, dcomp y)
{
dcomp result;

result = x + y;

return result;
}

and it works! But what I want is to store a complex number inside of a class! For this I define a class "parameters". For doubles, ints, etc, it's easy, but with complex numbers I don't know how to do it.
Here it's an example of what I want to do (but doesn't work).


//File with name parameters.h

#ifndef PARAMETERS_H
#define PARAMETERS_H

//typedef complex<double> dcmplx;

class parameters
{

public: parameters(); //constructor

//Simulation parameters
complex<double> variable;

private:

};

#endif

and

//File with name parameters.cpp
#include "Parameters.h"
#include <iostream>
#include <complex>
#include <cmath>

using namespace std;

//Initialization of the constructor

parameters::parameters()
{
//Constants of the system

variable = (2.0,3.0);
}

Can anyone help me? I'm desperate!!! I'm thinking in go back to fortran!!!

Thanks in advance
You can't initialise variables like that.

In c++ the expression (2.0 ,3.0) simply ignores the 2.0 and uses only the right most value (3.0).

The best way to initialise your complex number is in the ctor-initializer. After the constructor function name and parameter list you can put a comma separated list if initialisations after a colon:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <complex>

class parameters
{

public:
	parameters(); //constructor

	//Simulation parameters
	std::complex<double> variable;

private:

};

parameters::parameters()
: variable(2.0, 3.0) // initialize here in the ctor-initializer
{
    //... not here
}
Last edited on
You mean how to initialize?
1
2
3
parameters::parameters():variable(2,3){
    //...
}

Alternatively, you could have simply done this->variable=std::complex<double>(2,3);.
Hi!

It still not working. I have the 3 files described above:


main.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <complex>
#include "parameters.h"

using namespace std;

typedef complex<double> dcomp;

main()
{
	parameters par[1];
	
	cout << "Complex number = " << par[0].variable << endl;
		
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Parameters.h

#ifndef PARAMETERS_H
#define PARAMETERS_H

class parameters
{
	
	public: parameters(); //constructor

	//Simulation parameters	
	std::complex<double> variable;

	private:
	
 };
 
 #endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

//Parameters.cpp

#include "parameters.h"
#include <complex>

using namespace std;

parameters::parameters():
variable(2.0,3.0)

{
	
	//variable = (2.0,3.0);
	
}


and the compiler gives me:

parameters.h:20: error: ISO C++ forbids declaration of ‘complex’ with no type
parameters.h:20: error: invalid use of ‘::’
parameters.h:20: error: expected ‘;’ before ‘<’ token
parameters.cpp: In constructor ‘parameters::parameters()’:
parameters.cpp:15: error: class ‘parameters’ does not have any field named ‘variable’
nuno@toshiba-ubuntu:~/Research/ICMM/DDA_program/tests$




Do you know why?

Thanks
Last edited on
You're not including complex in the header.
Hey man, it's working!!!!

Many many thanks!!!

Sorry come back again, but if I want to use two or more variables?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Parameters.cpp

#include "parameters.h"
#include <complex>

using namespace std;

parameters::parameters():
variable(2.0,3.0);
variable2(1.0,1.0);

{
	
	//variable = (2.0,3.0);
	
}


doesn't work. Can you tell me why? Thanks for your help
I found the solution:
1
2
parameters::parameters():
variable(2.0,3.0), var(1.0,1.0)

Topic archived. No new replies allowed.