Search code examples
c++expressionlvalue

"Expression must be a modifiable LValue"


I am trying to create a physics simulation that simulates physics on all spaghettiObjects and displays those as text. There aren't currently any physics simulation, but those would go where //math is written

I am having trouble with this code. I'm a fairly new programmer and I started in java and am trying to learn c++. The errors are on line 48,49,50 where I'm trying to do the same thing with different method calls. I can't figure out what is wrong here, when I remove the = worldObjectToUpdate.variable it no longer throws an error.

Here is all of the code

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

Solution

  • createTestObject() = temp; most probably will be temp=createTestObject()