【发布时间】:2016-10-25 02:14:49
【问题描述】:
我正在尝试创建一个物理模拟,模拟所有 spaghettiObjects 上的物理并将其显示为文本。目前没有任何物理模拟,但那些会去写 //math 的地方
我在处理这段代码时遇到了问题。我是一个相当新的程序员,我从 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();
}
【问题讨论】:
-
还有,错误在哪一行?
-
你的代码有几个问题。
标签: c++ expression lvalue