Assignment to std::complex number

Z

zender

Hi,

Is it possible to a value separately to the real or imaginary
part of a complex number?

Any help appreciated,
Charlie

The following code shows that .real() and .imag() are not lvalues:

#include <iostream>
#include <complex>
int main()
{
std::complex<double> scalar;
scalar.real()=0.0;
}

zender@ashes:~/c++$ g++ -Wall -g -I${HOME}/include -o tst tst.cc
tst.cc: In function `int main()':
tst.cc:17: error: non-lvalue in assignment
 
M

Mike Wahler

Hi,

Is it possible to a value separately to the real or imaginary
part of a complex number?

I don't think so.
Any help appreciated,
Charlie

The following code shows that .real() and .imag() are not lvalues:

True. They return by value, not by reference.
#include <iostream>
#include <complex>
int main()
{
std::complex<double> scalar;
scalar.real()=0.0;

scalar = std::complex said:

-Mike
 
C

Charlie Zender

Thanks for the info!
Making real and imaginary component public and separatly assignable
would simplify many physics codes.

Charlie
 
J

John Carson

Hi,

Is it possible to a value separately to the real or imaginary
part of a complex number?

Any help appreciated,
Charlie

The following code shows that .real() and .imag() are not lvalues:

#include <iostream>
#include <complex>
int main()
{
std::complex<double> scalar;
scalar.real()=0.0;
}

For what it is worth VC++.Net offers the following "set function" as a
non-standard extension (presumably courtesy of the Dinkumware library that
it uses):

#include <iostream>
#include <complex>
int main()
{
std::complex<double> scalar;
scalar.real(0.0);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top