Assignment to std::complex number

Discussion in 'C++' started by zender@uci.edu, Jan 1, 2005.

  1. Guest

    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
    , Jan 1, 2005
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > 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<double>(0, scalar.imag());

    > }


    -Mike
    Mike Wahler, Jan 2, 2005
    #2
    1. Advertising

  3. Jonathan Turkanis, Jan 2, 2005
    #3
  4. Thanks for the info!
    Making real and imaginary component public and separatly assignable
    would simplify many physics codes.

    Charlie
    Charlie Zender, Jan 2, 2005
    #4
  5. John Carson Guest

    <> wrote in message
    news:
    > 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);
    }


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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page