首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向量搜索中的故障分割

向量搜索中的故障分割
EN

Stack Overflow用户
提问于 2017-10-25 22:02:11
回答 2查看 88关注 0票数 1

当我试图在向量中搜索一个特定的值时,我会得到一个分割错误。向量是人的类型

代码语言:javascript
复制
struct Person{
    string name;
    string address;
    string email;
    string number;
    string SocialSec;
    string other;
};

这是我的搜索功能:

代码语言:javascript
复制
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";
    }
}

下面是==和<<操作符的操作符重载:

代码语言:javascript
复制
ostream& operator<<(ostream &stream, const Person &it){
    stream << it;
    return stream;
}

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

分段错误是这样的:

代码语言:javascript
复制
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) 

做一个回溯:

代码语言:javascript
复制
#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<<重载以进行澄清。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-25 22:30:48

运算符应该打印Person类的基本类型。如下所示:

代码语言:javascript
复制
ostream& operator<<(ostream &stream, const Person &it){
    stream << "This is the name: " << it.name;
    return stream;
}

如果您确实在函数中流<<,它将继续在无限递归调用中打印Person。

票数 1
EN

Stack Overflow用户

发布于 2017-10-25 22:25:44

哦,对不起,我有点弄错了。以下是我的operator<<重载:

代码语言:javascript
复制
ostream& operator<<(ostream &stream, const Person &it){
    stream << it;
    return stream;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46942749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档