"Expression must be a modifiable LValue"
"Expression must be a modifiable LValue"
我正在尝试创建一个物理模拟,模拟所有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.propertyToModify
或pointerToObject->propertyToModify
。如果你希望它是一个自变量,lhs是variableToModify
。如果你想把它作为一个函数的参数,没有赋值表达式。在任何情况下,给函数调用表达式赋值都是没有意义的。
在这种情况下,似乎您正在尝试更新参数worldObjectToUpdate
的属性,但这也不起作用。原因是c++中的实参是按值传递的,因此将形参声明为SpaghettiObject
会告诉c++将整个对象复制到调用堆栈中——这不是您在java中所习惯的。然后,该新对象将在函数内部被修改,但是当函数返回时,该对象将消失。这就像打开一个文件,做一些编辑,然后关闭它而不保存。
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()
- 对RValue对象调用的LValue ref限定成员函数
- 为什么这些代码仍然使用 lvalue 函数?
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- "expression must be a modifiable lvalue"是什么意思?
- C++指针算术"lvalue required as left operand of assignment"错误
- 未执行lvalue到rvalue转换
- RVALUE参考与const lVALUE参考作为参数之间的混淆
- 声明规则定义了lvalue或prvalue
- 错误:无法将"std::istream {aka std::basic_istream<char>}"lvalue 绑定到"std::basic_istream&<char>&
- 与构造函数中rvalue结合的非const lvalue有关的错误
- RVALUE参考与LVALUE结合
- 移动lvalue语义
- 重载运算符 [] 并且没有收到"lvalue required as left operand of assignment"错误
- C++ 被迫做一个奇怪的演员表来摆脱"expression should be a modifiable lvalue"
- 产生错误"expression must be a modifiable lvalue"
- 如何实现对象指针的动态数组? "expression must be a modifiable lvalue"
- C++ 错误" expression must be a modifiable lvalue "
- C++ - "Expression must be modifiable lvalue"
- "Expression must be a modifiable LValue"