老实说,到目前为止,我什至不知道我要做什么。但是,直到我使该功能正常工作,我才能走得更远,并且每次都会抛出该异常,这是怎么回事?例外是“ CS 330 19S,P1,Calvert,程序1.exe中0x0F61CAB6(ucrtbased.dll)的未处理的例外:将无效参数传递给认为无效参数致命的函数”
#include<iostream>
#include<vector>
#include <fstream>
using namespace std;
struct Kinematic {
vector<vector<float>> position;
float orientation;
vector<vector<float>> velocity;
float rotation;
};
struct StreeringOutput {
vector<vector<float>> linear;
float angular;
};
void update(StreeringOutput steering, float time, Kinematic k)
{
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
k.position[i][j] += k.velocity[i][j] * time +
0.5*steering.linear[i][j] * time*time;
//the above command is where it throws the exception
k.velocity[i][j] += steering.linear[i][j] * time;
}
}
k.orientation += k.rotation*time + 0.5*steering.angular*time*time;
k.rotation = steering.angular*time;
}
int main()
{
int test;
Kinematic kin;
StreeringOutput steering;
float time = 0.0;
ofstream outfile;
outfile.open("Output.txt");
for (int i = 0; i < 100; i++)
{
update(steering, time, kin);
time += 0.5;
}
cin >> test;
return 0;
}
答案 0 :(得分:2)
程序启动时,您将创建对象:
Kinematic kin;
StreeringOutput steering;
这称为默认初始化,即所有成员都被初始化为默认值。对于vector
,它是空状态。
但是update
做这些事情:
// i and j can be as large as 99
k.position[i][j]
k.velocity[i][j]
steering.linear[i][j]
但是position
等是空的! vector
无法自动增长以适应您的需求。您正在索引越界,这是未定义的行为。
您应该正确初始化向量,以确保实际上有100个元素:
Kinematic kin;
kin.position.assign(100, vector<float>(100));
// same for others