Connecting Tech Pros Worldwide Forums | Help | Site Map

Assignment to std::complex number

zender@uci.edu
Guest
 
Posts: n/a
#1: Jul 23 '05
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


Mike Wahler
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Assignment to std::complex number



<zender@uci.edu> wrote in message
news:1104623521.120844.15190@z14g2000cwz.googlegro ups.com...[color=blue]
> Hi,
>
> Is it possible to a value separately to the real or imaginary
> part of a complex number?[/color]

I don't think so.
[color=blue]
>
> Any help appreciated,
> Charlie
>
> The following code shows that .real() and .imag() are not lvalues:[/color]

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

scalar = std::complex<double>(0, scalar.imag());
[color=blue]
> }[/color]

-Mike


Jonathan Turkanis
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Assignment to std::complex number


zender@uci.edu wrote:[color=blue]
> Hi,
>
> Is it possible to a value separately to the real or imaginary
> part of a complex number?[/color]

No, but there is support for adding this feature. See
http://www.open-std.org/jtc1/sc22/wg...004/n1589.html.

Jonathan


Charlie Zender
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Assignment to std::complex number


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

Charlie

John Carson
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Assignment to std::complex number


<zender@uci.edu> wrote in message
news:1104623521.120844.15190@z14g2000cwz.googlegro ups.com[color=blue]
> 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;
> }
>[/color]

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);
}


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Closed Thread