C++ 为什么在for循环中初始化类时会出现seg错误?
我的代码过去可以工作,但现在它开始给出seg错误。注释输出。推回(原子(温度[1]、stod(温度[2])、stod(温度[3])、stod(温度[4])、stod(温度[8]))停止输入。我在Windows10上的bash上运行它,并将乙醇mol2文件作为输入C++ 为什么在for循环中初始化类时会出现seg错误?,c++,segmentation-fault,C++,Segmentation Fault,我的代码过去可以工作,但现在它开始给出seg错误。注释输出。推回(原子(温度[1]、stod(温度[2])、stod(温度[3])、stod(温度[4])、stod(温度[8]))停止输入。我在Windows10上的bash上运行它,并将乙醇mol2文件作为输入 #include <iostream> #include <fstream> #include <sstream> #include <string> #include <vecto
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iterator>
using namespace std;
double Coulomb_k = 8.9875517873681764e9;
struct Atom{
string name;
double x;
double y;
double z;
double energy;
Atom(string n, double a, double b, double c, double d){
name = n;
x = a;
y = b;
z = c;
energy = d;
};
void printer(){
cout << "name: " << name << " pos x: " << x << " pos y: " << y << " pos z: " << z << " the energy: " << energy;
}
};
void mol2(std::istream& stream){
vector <Atom> input;
string a;
//skipping all the crap in the beginning
getline(stream, a);
getline(stream, a);
getline(stream, a);
istringstream iss(a);
std::vector<string> top_numbers{istream_iterator<string>{iss}, istream_iterator<string>{}};
cout << top_numbers[0] << endl;
//skipping more crap
getline(stream, a);
getline(stream, a);
stream >> a;
for(int i = 0; i < stoi(top_numbers[0]); i++){
getline(stream, a);
istringstream iss1(a);
std::vector<string> temp{istream_iterator<string>{iss1}, istream_iterator<string>{}};
input.push_back(Atom(temp[1], stod(temp[2]), stod(temp[3]), stod(temp[4]), stod(temp[8])));
}
}
int main(){
mol2(cin);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
双库仑k=8.9875517873681764e9;
结构原子{
字符串名;
双x;
双y;
双z;
双能量;
原子(字符串n,双a,双b,双c,双d){
name=n;
x=a;
y=b;
z=c;
能量=d;
};
作废打印机(){
cout正如crashmstr所说,是temp导致了这个问题。我通过修改代码(添加一些变量,直接存储到它们中,并使用它们启动类)来修复它:
向量mol2(标准::istream&stream){
矢量输入;
字符串a、名称、b、c、d;
双x,y,z,e;
//一开始就跳过所有的废话
getline(stream,a);
getline(stream,a);
getline(stream,a);
istringstream iss(a);
向量顶数{istream_迭代器{iss},istream_迭代器{};
库塔;
对于(int i=0;i>a>>名称>>x>>y>>z>>b>>c>>d>>e;
input.push_back(原子(名称,x,y,z,e));
}
返回输入;
}
如果您在调试器中单步执行该程序,您应该能够看到发生了什么以及它崩溃的原因(可能是带有temp
和索引的内容)。temp的大小是多少?我首先要验证所有tead操作是否都成功。我想不是。
@<TRIPOS>MOLECULE
ethanol
9 8 1
SMALL
USER_CHARGES
@<TRIPOS>ATOM
1 C1 -1.4803 -0.4530 1.4403 C.3 1 UNK 0.1199
2 C2 -2.7108 -1.3517 1.5701 C.3 1 UNK -0.1753
3 H3 -2.7552 -2.0859 0.7650 H 1 UNK 0.0698
4 H4 -2.7120 -1.8917 2.5176 H 1 UNK 0.0698
5 H5 -3.6265 -0.7616 1.5273 H 1 UNK 0.0698
6 O6 -0.3206 -1.2582 1.5006 O.3 1 UNK -0.6589
7 H7 -1.4579 0.2880 2.2417 H 1 UNK 0.0432
8 H8 -1.5011 0.0948 0.4962 H 1 UNK 0.0432
9 H9 0.4535 -0.6787 1.4172 H 1 UNK 0.4187
@<TRIPOS>BOND
1 1 2 1
2 1 6 1
3 1 7 1
4 1 8 1
5 2 3 1
6 2 4 1
7 2 5 1
8 6 9 1
@<TRIPOS>SUBSTRUCTURE
1 UNK 1 GROUP 0 **** 0 ROOT
vector<Atom> mol2(std::istream& stream){
vector <Atom> input;
string a, name, b, c, d;
double x, y, z, e;
//skipping all the crap in the beginning
getline(stream, a);
getline(stream, a);
getline(stream, a);
istringstream iss(a);
std::vector<string> top_numbers{istream_iterator<string>{iss}, istream_iterator<string>{}};
cout << top_numbers[0] << endl;
//skipping more crap
getline(stream, a);
getline(stream, a);
stream >> a;
for(int i = 0; i < stoi(top_numbers[0]); i++){
cin >> a >> name >> x >> y >> z >> b >> c >> d >> e;
input.push_back(Atom(name, x, y, z, e));
}
return input;
}