2016-11-13 9 views
0
#include <iostream> 
#include <vector> 
#include <fstream> 
using namespace std; 

struct Point 
{ 
    double x; 
    double y; 
}; 

istream& operator>>(istream& is, Point& p) 
{ 
    char ch; 
    if(is >> ch && ch != '(') 
    { 
     is.unget(); 
     is.clear(ios_base::failbit); 
     return is; 
    } 

    char ch2; 
    double x; 
    double y; 

    is >> x >> ch >> y >> ch2; 
    if(!is || ch != ';' || ch2 != ')') 
    { 
     cerr << "Error: Bad record!\n"; 
     exit(1); 
    } 
    p.x = x; 
    p.y = y; 
} 

ostream& operator<<(ostream& os, const Point& p) 
{ 
    return os << '(' << p.x 
    << ';' << p.y << ')' << endl; 
} 

int main() 
{ 
    Point p; 
    vector<Point> original_points; 
    cout << "Please enter 3 points:\n"; 
    for(int i = 0; i < 3; ++i) 
    { 
     cin >> p; 
     original_points.push_back(p); 
    } 

    cout << "\nYou've entered:\n"; 

    for(Point x : original_points) 
     cout << x; 

    ofstream ost{"mydata"}; 
    for(Point x : original_points) 
     ost << x; 
    ost.close(); 

    vector<Point> processed_points; 
    ifstream ist{"mydata"}; 
    while(ist >> p) 
     processed_points.push_back(p); 
    ist.close(); 

    cout << "original_points: "; 
    for(Point x : original_points) 
     cout << x; 

    cout << "processed_points: "; 
    for(Point x : original_points) 
     cout << x; 

    if(original_points.size() != processed_points.size()) 
     cout << "Oops! Seems like something went wrong!\n"; 
    return 0; 
} 

デバッグした後、私はエラーがこのコード行によって引き起こされることを考え出した:セグメンテーションフォールト読み取りファイル

while(ist >> p) 

は、コードのこの部分は、本からコピーされたほぼ100%:

istream& operator>>(istream& is, Point& p) 
{ 
    char ch; 
    if(is >> ch && ch != '(') 
    { 
     is.unget(); 
     is.clear(ios_base::failbit); 
     return is; 
    } 

    char ch2; 
    double x; 
    double y; 

    is >> x >> ch >> y >> ch2; 
    if(!is || ch != ';' || ch2 != ')') 
    { 
     cerr << "Error: Bad record!\n"; 
     exit(1); 
    } 
    p.x = x; 
    p.y = y; 
} 

Googleとstackoverflowは、このエラーは間違った方法でメモリにアクセスすることによって発生したと言います。私はこのコードを1時間チェックしていましたが、何が問題を引き起こすのか理解できません。私は今日ストリームを勉強し始めました。これは「Programming - C++を使った原則と実践(第2版)」の第10章での演習です。

P.S.私の英語の文法のため申し訳ありませんが、それは私の母国語ではありません)機能operator>>(istream& is, Point& p)リードでコードの

+0

あなたの '' 'オーバーロードに入り、segfaultの発生場所を判断する必要があります。 –

+0

時間は非常に短い時間です。それを続けてください。数日後に戻ってください。 –

+0

さて、私はこのコードを書いていたときに純粋なコンソールを持っていました。私は上記のアドバイスに従い、うまくいけば解決策を投稿します。 – SweetYogurt

答えて

0

ないすべてのブランチ最終値を返すことに私はあなたが機能

の終わりにプッシュ返す値 return is;を忘れてしまっていると仮定
istream& operator>>(istream& is, Point& p) 
{ 
    char ch; 
    if(is >> ch && ch != '(') 
    { 
     is.unget(); 
     is.clear(ios_base::failbit); 
     return is; 
    } 

    char ch2; 
    double x; 
    double y; 

    is >> x >> ch >> y >> ch2; 
    if(!is || ch != ';' || ch2 != ')') 
    { 
     cerr << "Error: Bad record!\n"; 
     exit(1); 
    } 
    p.x = x; 
    p.y = y; 
    return is; 
} 
+0

ええと、私はこれを本当にすぐに見つけました。私はC :: Bを試してみました。コンパイラはすぐに私に警告しました。しかし、別の問題が発生しました。私のエラーハンドラは、最後の値が赤のプログラムが再び>>関数に入り、 "Error:Bad record!\ n"というエラーにつながることがわかったので、デバッグ中にファイルの終わりなどを認識できませんでした。すべての値が正しく赤で、ベクトルに格納されます。最初に、私はEOFに達したときに(ist >> p)が偽になると思った。第2に、if(&gt; ch && ch!= '(')は、プログラムが2番目に達するようにしてはいけません。 – SweetYogurt

関連する問題