C++迭代器iterator段错误segmentation fault (core dumped)
来源:互联网 发布:淘宝宝贝截图 编辑:程序博客网 时间:2024/05/28 15:36
调试数据层中的Storage
类时出现segmentation fault (core dumped),最终将错误源头锁定在Storage
类一个成员函数中的这段代码:
//"A","B&C"//faultfor(auto x:m_meetingList){ m<<"\""<<x.getSponsor()<<"\",\""; for(auto iter=x.getParticipator().begin();iter!=x.getParticipator().end();++iter){ cout<<*iter; /*auto temIter=iter; if(++temIter!=x.getParticipator().end()) cout<<"&"; */ } std::cout<<std::endl;}
错误之处:
auto iter=x.getParticipator().begin();iter!=x.getParticipator().end()
解决方法,改成这样:
std::vector<std::string> participators=x.getParticipator();for(auto iter=participators.begin();iter!=participators.end();++iter)
全貌:
//"A","B&C"//correctfor(auto x:m_meetingList){ m<<"\""<<x.getSponsor()<<"\",\""; std::vector<std::string> participators=x.getParticipator(); for(auto iter=participators.begin();iter!=participators.end();++iter){ cout<<*iter; /*auto temIter=iter; if(++temIter!=participators.end()) cout<<"&"; */ } std::cout<<std::endl;}
错误原因:
auto iter=x.getParticipator().begin();iter!=x.getParticipator().end()
x.getParticipator()
返回时隐式调用了拷贝构造函数,两个x.getParticipator()
就调用了两次拷贝构造函数,前后两个x.getParticipator()
并不是同一vector对象,引起异常。
说明:
m_meetingList
是Storage
类的私有成员,存放Meeting
类的对象。
private: std::list<Meeting> m_meetingList;
Meeting
类的部分私有成员:赞助者的名字,参与者的名单
private: std::string m_sponsor; std::vector<std::string> m_participators;
getparticipator()
是Meeting
类的公有成员函数,复制m_participators
,返回出来。
/*** @brief get the participators of a meeting * @return return a string vector indicate participators*/std::vector<std::string>getparticipator(void) const;
阅读全文
1 0
- C++迭代器iterator段错误segmentation fault (core dumped)
- Segmentation fault (core dumped)(段错误)
- Segmentation fault (core dumped) --段错误
- 再谈Segmentation fault (core dumped)问题 -查找段错误原因
- Ubuntu--(8)段错误Segmentation fault (core dumped)
- 关于Segmentation fault (core dumped)的错误
- Segmentation fault (core dumped)
- Segmentation fault (core dumped)
- Segmentation fault (core dumped)
- Segmentation fault (core dumped)
- Segmentation fault (core dumped)
- Segmentation fault (core dumped)
- Segmentation fault (core dumped)
- Segmentation fault (core dumped)
- segmentation fault (core dumped)
- Segmentation fault (core dumped)
- Segmentation fault (core dumped)
- Segmentation fault (core dumped)
- PAT--1035. Password
- NYOJ 5 Binary String Mathing (substr函数)
- poj 2586
- 5_android的界面编程(UI)
- auto和decltype
- C++迭代器iterator段错误segmentation fault (core dumped)
- D.Innokenty and a football league
- 07-python学习笔记-列表生成式
- java多线程
- 程序员面试金典——n皇后问题_____(S)
- UDA1341(asoc)分析
- 【IO流】FileReader,FileWriter完成文件的copy
- Python入门学习(3)
- MySQL大数据量分页查询方法及其优化