'Why this assignable expression?

New to c++, and I have this code I want to run but it's showing expression is not assignable i can put here a smaller piece of code that is similar to what I'm building,

int main(){
    complex<double> constant(0,0);

    real(constant)=1/3;

    cout << constant;



    return 0;
}

Now when you build it, it shows the expression mentioned above:

WW.cpp:83:26: error: expression is not assignable real(constant) = 1

c++


Solution 1:[1]

The problem is that the call expression real(constant) in your example, is an rvalue of type double and so cannot be used on the left hand side of an assignment.

If you want to set the real component then you can pass the value to the member function real as shown below:

//------------vv---->pass the value here
constant.real(15); 

Demo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1