为什么这个程序告诉我传递了无效参数
Why is this program telling me invalid parameter was passed?
老实说,我甚至不知道我现在要做什么。但我不能更进一步,直到我让这个函数工作,它每次都抛出异常,出了什么问题?异常为"CS 330 19S,P1,Calvert,Program 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;
}
程序启动时,创建对象:
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
相关文章:
- 为什么 CRTP 模板C++给出无效参数错误?
- 为什么此字符串在传递到stoi时被视为无效参数
- boost::asio::async_write无效参数
- C ++回测问题:如何检查构造函数在假定失败时是否失败(给定输入的无效参数)
- 无效参数 C++ cygwin
- 套接字接受无效参数 (c++)
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 将无效参数传递给将无效参数视为致命的函数
- 如何避免字符串到整数转换情况下的无效参数异常
- 张量流 无效参数:In[0] 不是矩阵
- cudaFreeHost() 无效参数错误
- 未经治疗的例外,无效的参数传递给了一个认为无效参数作用的函数
- C++带有可变参数模板的无效参数
- std::find是否隐式修复无效参数
- 为什么这个程序告诉我传递了无效参数
- 在Win32中获取SetConsolecursorPosition函数的ERRORRORNO 87(无效参数)
- MSVCR120.dll的无效参数错误(在64位窗口中构建Tesseract Lib)
- 在C++中使用字符串类的 .substr 获取无效参数
- MySQL++ 无效参数错误
- 搜索无效参数