Home > front end >  Why this assignable expression?
Why this assignable expression?

Time:04-27

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

CodePudding user response:

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

CodePudding user response:

The value you want to assign to real(constant) is not possible due to this complex constant(0,0) you assigned the value here with the default value.

  •  Tags:  
  • c
  • Related