2017-10-25 9 views
1

ベクトルの特定の値を検索しようとしているときにセグメント化エラーが発生しています。ベクターは、ここではPerson型のベクトルを検索する際のセグメント化エラー

struct Person{ 
    string name; 
    string address; 
    string email; 
    string number; 
    string SocialSec; 
    string other; 
}; 

である私の検索機能である:どのようにこれは

ostream& operator<<(ostream &stream, const Person &it){ 
    stream << it; 
    return stream; 
} 

    bool operator==(const Person &lhs, const string &rhs){ 
     return lhs.name == rhs;  
    } 

:ここ

void searchContact(vector<Person> &people) { 
    string searchTerm; 
    cout << endl; 
    cout << "Enter search term: "; 
    getline(cin, searchTerm); 
    vector<Person>::iterator it=find(people.begin(), people.end(), searchTerm); 

    if (it != people.end()){ 
     cout << *it; 
    }else{ 
     cout << "Element not found\n"; 
    } 
} 

は==と< <事業者のための演算子オーバーロードですセグメント化障害は次のようになります。

Program received signal SIGSEGV, Segmentation fault. 
0x00005555555565ae in operator<< (
    stream=<error reading variable: Cannot access memory at address 0x7fffff7feff8>, 
    it=<error reading variable: Cannot access memory at address 0x7fffff7feff0>) at class.cpp:114 
114 ostream& operator<<(ostream &stream, const Person &it){ 
(gdb) 

バックトレースを行う:

#1 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#2 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#3 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#4 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#5 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#6 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#7 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#8 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#9 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#10 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#11 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#12 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#13 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#14 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#15 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#16 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#17 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 
#18 0x00005555555565c9 in operator<< (stream=..., it=...) at class.cpp:115 

は、なぜこれが起こっている、そしてどのように私はこの問題を解決することができますか? スタックオーバーフローですか?

EDIT:明確にするために、元のポストにオペレータ< <を追加しました。

+5

これは間違いなくスタックのオーバーフローであり、検索とは関係ありません。バグの 'operator <<'です。私たちが助けてくれるようにしたい場合は、コードを記入してください。 – Quentin

+2

<< Person >>を受け入れるためにオーバーロードされた<<演算子のようですか?あなたはそれを提供できますか? – Valentin

+3

あなたが言う*ここで==と<<演算子の演算子のオーバーロードは:*ですが、投稿されたコードには演算子<<がありません。 –

答えて

1

あなたのオペレータがあなたのPersonクラスのプリミティブ型を印刷する必要があります。ここに私のオペレータは< <オーバーロードです。このように:あなたはあなたの関数の内部で< <にそれをストリーム行う場合

ostream& operator<<(ostream &stream, const Person &it){ 
    stream << "This is the name: " << it.name; 
    return stream; 
} 

、それは無限の再帰呼び出しで人を印刷しようとし続けます。

+0

Sweet lord、savior。助けてくれてありがとう。私はその脳全体を徹底的に排水しました。 – Shitpost12

+0

ああ私はそれが起こることを知って、あなたは歓迎です! –

0

ああ申し訳ありませんが、私は少し間違った貼り付けコピー。

ostream& operator<<(ostream &stream, const Person &it){ 
    stream << it; 
    return stream; 
} 
+2

このコードは単に自分自身を呼び出すため、スタックのオーバーフローが発生します。 – pm100

関連する問題