"Expression must be a modifiable LValue"

"Expression must be a modifiable LValue"

本文关键字:modifiable LValue be Expression must      更新时间:2023-10-16

我正在尝试创建一个物理模拟,模拟所有spaghettiObjects上的物理并将其显示为文本。目前还没有任何物理模拟,但这些会去//math写的地方

我对这段代码有麻烦。我是一个相当新的程序员,我从java开始,并试图学习c++。错误在第48、49、50行,我试图用不同的方法调用做同样的事情。我无法找出什么是错误的在这里,当我删除= worldobjecttouupdate。变量,则不再抛出错误。

下面是所有的代码

#include <iostream>
#include <string>
using namespace std;
//Created to hold the x, y, velocity and mass of an object.  
//It's called a spaghettiObject cause I was bored.
//A better name would probably be something like "worldObject"
struct spaghettiObject {
public:
    float velocity;
    float positionX;
    float positionY;
    float mass;
};
//All world objects in an array
spaghettiObject myArray[5];
//test zone
spaghettiObject test;
spaghettiObject createTestObject() {
    myArray[1] = test;
    test.mass = 2;
    test.positionX = 2;
    test.positionY = 2;
    test.velocity = 2;
    return test;
}
//Output from the physics simulation to the render function
spaghettiObject output;
//Calculates the position of a spaghettiObject's x using velocity
float nextPositionX(spaghettiObject objectToTransform) {
    float objectToTransformNewX = objectToTransform.positionX;
    //math
    return  objectToTransformNewX;
}
//Calculates the position of a spaghettiObject's x using velocity
float nextPositionY(spaghettiObject objectToTransform) {
    float objectToTransformNewY = objectToTransform.positionY;
    //math
    return objectToTransformNewY;
}
//Calculates the new velocity. for use:after updating x and y of object
float nextVelocity(spaghettiObject objectToUpdate) {
    float objectToUpdateNewV = objectToUpdate.velocity;
    //math
    return objectToUpdateNewV;
}
//Designed to be where all of the update functions are called
void updateWorldObject(spaghettiObject worldObjectToUpdate) {
    nextPositionX(worldObjectToUpdate) = worldObjectToUpdate.positionX;
    nextPositionY(worldObjectToUpdate) = worldObjectToUpdate.positionY;
    nextVelocity(worldObjectToUpdate) = worldObjectToUpdate.velocity;
}
//calculates position of variable defined above, myArray
void nextUpdateOfMyArray() {
    //temp object that is effectively the entire array
    spaghettiObject temp;
    //initilization of temp
    temp.mass = 0;
    temp.positionX = 0;
    temp.positionY = 0;
    temp.velocity = 0;
    //for calling functions for
    //each of the objects
    for (int i = 0; i++; i < (sizeof myArray / sizeof spaghettiObject)) {       
        //assigning the current object in the array to the temp object
        myArray[i] = temp;
        //Calling the update functions
        updateWorldObject(temp);
        //test
        createTestObject() = temp;
        //regular code
        output = temp;
    }
}
//Used to update the physics
void update() {
    nextUpdateOfMyArray();
}
//Used to render a console output
void render() {
    cout << output.mass + output.positionX + output.positionY + output.velocity;
}
int main() {
    update();
    render();
    cin.get();
}

updateWorldObject中的functionCall() = value是你的问题。你不能给函数调用赋值。你打算让这个值去哪里?如果您希望它在对象中,则赋值的lhs应该是objectToModify.propertyToModifypointerToObject->propertyToModify。如果你希望它是一个自变量,lhs是variableToModify。如果你想把它作为一个函数的参数,没有赋值表达式。在任何情况下,给函数调用表达式赋值都是没有意义的。

在这种情况下,似乎您正在尝试更新参数worldObjectToUpdate的属性,但这也不起作用。原因是c++中的实参是按值传递的,因此将形参声明为SpaghettiObject会告诉c++将整个对象复制到调用堆栈中——这不是您在java中所习惯的。然后,该新对象将在函数内部被修改,但是当函数返回时,该对象将消失。这就像打开一个文件,做一些编辑,然后关闭它而不保存。

相反,您希望将形参声明为指向对象的引用。在java中,所有对象都是引用,因此您永远不会做出这种区分。在c++中,有两种语法方法可以表明形参是引用,这样函数就会修改传递给它的对象,而不是克隆它并丢弃所做的更改。
void function(Object* pointerToObject) {
    pointerToObject->propertyToModify = newValue;
}
Object anObject;
function(&anObject);

在这种情况下,引用是指针。它指向您正在使用的对象。当将指针作为参数复制到堆栈中时,对象本身不会被复制,并且函数可以使用->而不是.来更改其属性或调用其方法。必须像&anObject一样,使用操作符&获取指向对象的指针。

另一种引用对象的语法方式更接近于java中使用的方式。

void function(Object& objectToModify) {
    objectToModify.propertyToModify = newValue;
}
Object anObject;
function(anObject);

Object&类型是对和Object引用,这几乎是你从java中想到的:你仍然使用.操作符,你从不做任何特殊的事情来创建引用,对象不被复制。引用(与指针相反)的缺点是,一旦引用引用了一个对象,它就不能被重新赋值给另一个对象,但作为函数的形参,它就可以了。

createTestObject() = temp;最有可能是temp=createTestObject()