问题是迭代器没有遍历循环。我不知道为什么。 #includes在标题中就像我的惯常一样。
#include "neutronFileReader.h"
using namespace std ;
neutronFileReader::neutronFileReader()
{
}
list<vector<float> > neutronFileReader::spectrum(char* filename)
{
ifstream fin(filename) ;
string binhi, binlo ;
list<vector<float> > neutronSpectrum ;
list<vector<float> >::iterator nS ;
vector<float> EnergyProbability ;
while(!fin.eof())
{
getline(fin, binlo, ' ') ; //get the binlo string
cout << "binlo: "<<binlo << endl ;
getline(fin, binhi, ' ') ; //get the binhi string
cout<<"binhi: "<<binhi<<endl ;
EnergyProbability.push_back(atof(binhi.c_str())+(atof(binhi.c_str()) - atof(binlo.c_str()))/2) ; //store middle of bin as emission Energy
getline(fin, binlo) ; //try not to waste memory space
cout<<"prob: "<<binlo<<endl ;
EnergyProbability.push_back(atof(binlo.c_str())) ; //store emnission probability
neutronSpectrum.push_back(EnergyProbability) ; //put the vector in the list
//cout<<neutronSpectrum<<endl ;
}
for(nS = neutronSpectrum.begin() ; nS != neutronSpectrum.end() ; nS++) //go through the neutron spectrum
{
EnergyProbability = (*nS) ;
cout << "binval: " << EnergyProbability[0] << " " << "binProb: " << EnergyProbability[1] << endl ;
cout << "binval: " << (*nS)[0] << ", binprob: " << (*nS)[1] << ", memadd: " << &nS << endl ; // print energy & prob to screen
}
return neutronSpectrum ;
}
无论如何,这里的一些帮助将非常感激,已经将它移动到while循环中,是的,这是所有错误测试,但它是一个相当重要的代码。干杯,总是在学习。
答案 0 :(得分:1)
你确定要填充neutronSpectrum
数组,所以它不是空的吗?请确保将其添加到最后:
if (neutronSpectrum.empty())
cerr << "error: empty neutronSpectrum" << endl;
您的输入文件可能存在问题(它是空的或不可读的),因此您最初不会向neutronSpectrum
添加任何内容。为了确保,请在cout
循环中添加一些while
语句。另外值得在fin.error()
循环后检查while
:
if (fin.error())
cerr << "error: error reading input file: " filename << endl;
答案 1 :(得分:1)
您没有清除输入循环之间的EnergyProbability。 (* ns)[0]因此看到来自第一个输入的冗余存储值,忽略实际在[2]中的新值,然后[4]等。只需在读取更多值之前添加EnergyProbability.clear()
它
答案 2 :(得分:0)
如果for
中没有打印任何内容,那么结尾neutronSpectrum
可能是空的。
答案 3 :(得分:0)
好的,已经检查了以上所有内容,一个问题是我没有清除while循环的每次迭代的EnergyProbability向量(不是它是空的),这是创建一个越来越大的向量被推到后面每次都是列表的列表,所以输出了指向列表元素(A向量)的前两个元素,每次都是相同的(显然,我是个傻瓜)。所以,现在我知道问题实际上是迭代器没有意识到它已经到达列表的末尾,而且当它发生时它会抛出一个空指针,我想。代码在这里:
#include "neutronFileReader.h"
using namespace std ;
neutronFileReader::neutronFileReader()
{
}
list<vector<float> > neutronFileReader::spectrum(char* filename)
{
ifstream fin(filename) ;
string binhi, binlo ;
list<vector<float> > neutronSpectrum ;
list<vector<float> >::iterator nS ;
vector<float> EnergyProbability ;
while(!fin.eof())
{
EnergyProbability.clear() ;
getline(fin, binlo, ' ') ; //get the binlo string
cout << "binlo: "<<binlo << endl ;
getline(fin, binhi, ' ') ; //get the binhi string
cout<<"binhi: "<<binhi<<endl ;
EnergyProbability.push_back(atof(binhi.c_str())+(atof(binhi.c_str()) - atof(binlo.c_str()))/2) ; //store middle of bin as emission Energy
getline(fin, binlo) ; //try not to waste memory space
cout<<"prob: "<<binlo<<endl ;
EnergyProbability.push_back(atof(binlo.c_str())) ; //store emnission probability
cout << EnergyProbability[0] << ":" << EnergyProbability[1] << endl ;
neutronSpectrum.push_back(EnergyProbability) ; //put the vector in the list
}
for(nS = neutronSpectrum.begin() ; nS != neutronSpectrum.end() ; nS++) //go through the neutron spectrum
{
EnergyProbability = (*nS) ;
cout << &neutronSpectrum.begin() << " : " << &nS << " : " << &neutronSpectrum.end() << endl ; // print energy & prob to screen
}
return neutronSpectrum ;
}
输出是:
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
0x28fbcc : 0x28fba8 : 0x28fbc8
然后code :: blocks必须意识到它处于无限循环中,没有任何变化并退出代码。
欢呼人们