2017-06-22 9 views
0

私のコードを使用していましたが、今はsegフォールトエラーが発生し始めます。 input.push_backのコメントアウト(Atom(temp [1]、stod(temp [2])、stod(temp [3])、stod(temp [4])、stod(temp [8])))は、私はWindows 10でbash上で実行し、入力としてエタノールmol2ファイルを取る。forループでクラスを初期化するときにseg faultが発生するのはなぜですか?

#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; 
} 

入力:

@<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  
+0

デバッガでプログラムをステップ実行すると、次のようにコードを修正することで修正できます(この変数を追加して、それらに直接格納できます)。何が起きているのか、なぜクラッシュしているのでしょうか(たぶん 'temp 'とそれにインデックスを付けたもの)。 – crashmstr

+0

'temp'のサイズは? – NathanOliver

+1

私は、すべてのティッド操作が成功することを確認することから始めます。彼らはそうではないと思います。 –

答えて

0

crashmstrが言ったように、それが問題を引き起こしていた一時でした。

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; 
} 
関連する問題