I have a class called CircuitProfiler that has a public member function switchState. Below is the code for it:
void CircuitProfiler::switchState(int index) { if (profilingNode) { switch (index) { case 0: node->getVoltage().isKnown = !node->getVoltage().isKnown; break; } } } The code I am having trouble with is the line:
node->getVoltage().isKnown = !node->getVoltage().isKnown;
I am getting error that says "expression must be a modifiable lvalue". There is a red line under node.
node is a pointer to an instance of a Node class that I created. node a private data member of CircuitProfiler. It is declared in CircuitProfiler as Node* node;
Node has a private data member called voltage, and getVoltage is a public member function of Node that simply returns voltage. voltage is declared in Node as VariableValue<float> voltage; and the code for getVoltage is:
VariableValue<float> Node::getVoltage() { return voltage; } voltage is an instance of a simple struct called VariableValue, the code for which is below.
template <class T> struct VariableValue { T value; bool isKnown; }; So why am I getting the error "expression must be a modifiable value"? Let me know if I need to provide any more information.
VariableValue<float>& Node::getVoltage(), i.e., return by reference. Otherwise you'd be modifying a temporary in the=call.GetVoltage()returns by value so it returns an rvalue (loosely, a value that is on the right of an assignment expression). Among other things that means the returned value will cease to exist at the end of the current statement, and modifications to it are meaningless. C++, by design, treats such deeds as a diagnosable error. If you want to modify the underlyingvoltageobject that it returns, change the function so it returns a reference, not by value.