【问题标题】:Segmentation fault when searching in vector在向量中搜索时出现分段错误
【发布时间】:2017-10-25 22:02:11
【问题描述】:

当我尝试在向量中搜索特定值时遇到分段错误。向量的类型为 Person

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

这是我的搜索功能:

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

这里是 == 和

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

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

分段错误如下所示:

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

为什么会发生这种情况,我该如何解决? 是堆栈溢出吗?

编辑:在原始帖子中添加了运算符

【问题讨论】:

  • 这绝对是堆栈溢出,与您的搜索无关:这是您的operator&lt;&lt; 有问题。如果您希望我们能够提供帮助,请发布其代码。
  • 看起来你重载了 Person?可以提供吗?
  • 你说 这里是 == 和 但发布的代码没有operator&lt;&lt;
  • 看起来您的 &lt;&lt; 运算符中有无限递归。
  • @user4581301 我现在修好了,抱歉我有点累了...

标签: c++ segmentation-fault


【解决方案1】:

您的操作员应该打印您的 Person 类的原始类型。像这样:

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

如果你在你的函数中流

【讨论】:

  • 亲爱的主,救世主。十分感谢你的帮助。我在那个方面完全是人才流失。
【解决方案2】:

哦,对不起,复制粘贴我有点错误。这是我的运算符

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

【讨论】:

  • 这段代码只是调用自己,因此堆栈溢出
猜你喜欢
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
相关资源
最近更新 更多