"Expression must be a modifiable LValue"

"Expression must be a modifiable LValue"

我正在尝试创建一个物理模拟来模拟所有 spaghettiObjects 上的物理并将它们显示为文本。目前没有任何物理模拟,但那些会去哪里//math written

我在使用这段代码时遇到问题。我是一个相当新的程序员,我从 java 开始,正在尝试学习 C++。错误在第 48、49、50 行,我试图用不同的方法调用做同样的事情。我不知道这里出了什么问题,当我删除 = worldObjectToUpdate.variable 它不再抛出错误。

这是所有代码

#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();
}

createTestObject() = temp; 很可能会是 temp=createTestObject()

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

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

相反,您想将参数声明为对象的 引用。在 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 的内容:你仍然使用 . 运算符,你从不做任何特殊的事情来创建引用,对象不会被复制。引用(与指针相反)的缺点是,一旦引用引用了一个对象,它就不能重新分配给另一个对象,但作为函数的参数就可以了。