写入文件时,矢量下标超出范围
vector subscript out of range when writing to file
我想写一个向量到一个文件,它表示一个函数的结果,该函数以"double Golf_dl"的步长增加j。vector本身必须包含"int Golf_NL"元素。(Golf_dl是类Tsunami的私有成员)问题是我得到一个所有值都等于0.5的常量向量,这是垃圾。有人能看出哪里不对吗?
代码:void T_Tsunami::Toestand(int nl)
{
struct pb
{
std::vector<double>& v_;
pb(std::vector<double>& v)
: v_(v){};
pb& operator+(double i)
{
v_.push_back(i);
return *this;
}
};
for( double i = 0; i < nl; i++ )
{
double j=0;
double y = 0.25*(1-tanh(double(j-75)/5));
j+=Golf_dl;
pb(T_1)+y;
}
}
void T_Tsunami::Write(vector<double>d)
{
const int Naamgrootte=64;
char Naam[Naamgrootte];
std::cout << "Geef een bestandsnaam in" << std::endl;
std::cin >> Naam;
std::cout << std::endl;
std::ofstream outFile(Naam);
if (!outFile)
{
std::cerr << "Kan bestand niet openen" << std::endl;
exit (1);
}
for (int i=0; i<100; i++)
{
outFile << d[i] << std::endl;
}
outFile.close();
}
你的问题在这里:
for( double i = 0; i < nl; i++ )
{
double j=0;
double y = 0.25*(1-tanh(double(j-75)/5));
j+=Golf_dl;
pb(T_1)+y;
}
考虑这样一个事实,即循环变量i
没有在循环内的任何地方使用。此外,j
和y
在循环之间不保留它们的值(它们不是static
)。
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 矢量下标超出SFML游戏中的范围
- 运行时错误:矢量下标超出范围:正在检查空集
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 编译期间矢量下标超出范围
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 对矢量来说是全新的.矢量下标超出范围
- C++十进制到十六进制(字符串下标超出范围)
- 创建结构体向量,表达式:向量下标超出范围
- 读取矢量时矢量下标超出范围
- 断言失败错误,C++中的矢量下标超出范围问题
- 如何在 Linux 上强制执行矢量下标超出范围的调试断言
- 表达式:矢量下标超出范围
- 矢量下标超出范围,带有嵌套的 for 循环
- 向量下标超出向量向量的范围
- gcc8.2如何启用警告:数组下标在数组范围之上[-warray-bounds]
- 向量下标出的范围错误.即使向量的索引大于访问数据的索引,也会发生误差
- std::vector<std::vector<int>>:调试断言失败。C++矢量下标超出范围保留内存
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- C++超出下标范围