for_each算法引起basic_string :: _ m_construct null在C 中无效
for_each algorithm causes basic_string::_M_construct null not valid in C++
在学习C 的项目中,我创建了一个由两个简单类组成的软件(家和人)。人们有构造函数:
// CONSTRUCTOR
People(): name("NoName"), first_name("NoFirstName"), age(0){}
People(std::string n, std::string fn, int a) : name(n), first_name(fn), age(a){}
和家有:
// CONSTRUCTOR
Home(): adresse("NoName"){}
Home(std::string addr): adresse(addr){}
在我的软件中,一个房屋有一个人,我们可以在其中添加居民或删除居民。
当我尝试删除房屋中的居民或尝试打印房屋时,我的错误发生。
在这里," Removeresident"的代码:
void Home::removeHabitant(People const &p)
{
this->getHabitant().erase(std::remove(this->getHabitant().begin(), this->getHabitant().end(), p));
}
在这里,"运营商<<"的代码:
std::ostream & operator<<(std::ostream & out, Home const &h)
{
out << h.getAddr() << "n"; //OK
if(h.getHabitant().size() > 0) // OK
{
try
{
std::for_each(h.getHabitant().begin(), h.getHabitant().end(), [&out](People const pe){
out << pe << "n";
}); // ERROR
}
catch(People p)
{
std::cout << "Exception à l'element : " << p << std::endl;
}
}
else // OK
{
out << "Aucun habitant !"; // OK
}
return out ; // OK }
在这里我的软件的输出:
clang++ -Wall -std=c++11 -c -o obj/main.o src/main.cpp -I include
clang++ -Wall -std=c++11 -c -o obj/People.o src/People.cpp -I include
clang++ -Wall -std=c++11 -c -o obj/Home.o src/Home.cpp -I include
clang++ -Wall -std=c++11 -o bin/main obj/main.o obj/People.o obj/Home.o
./bin/main
Peoples's destructor
( NoFirstName - NoName - 0 )
10 rue des Brouettes rouge
Peoples's destructor
Peoples's destructor
( Erwan - AUBRY - 21 )
Peoples's destructor
( Roger - DURAND - 20 )
Peoples's destructor
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
makefile:6: recipe for target 'compile' failed
make: *** [compile] Aborted
在这里主文件:
#include <Home.hpp>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
People erwan("AUBRY", "Erwan", 21);
People roger("DURAND", "Roger", 20);
People noName;
// vector<People> lsPeople;
// lsPeople.push_back(erwan);
// lsPeople.push_back(roger);
// copy(lsPeople.begin(), lsPeople.end(), ostream_iterator<People>(cout, "|"));
Home home1("10 rue des Brouettes rouge");
home1.addHabitant(erwan);
home1.addHabitant(roger);
cout << noName << endl;
cout << home1 << endl;
// cout << home1[0] << endl;
// home1.removeHabitant(roger);
// cout << home1[0] << endl;
return 0;
}
经过几项研究,我认为这是家庭课程的原因,所以这是家庭守则.hpp:
#ifndef HOME_INCLUDED
#define HOME_INCLUDED
#include <People.hpp>
#include <vector>
class Home
{
private:
std::string adresse;
std::vector<People> habitant;
public:
// CONSTRUCTOR
Home(): adresse("NoName"){}
Home(std::string addr): adresse(addr){}
// DESTRUCTOR
~Home(){std::cout << "Home's destructor" << std::endl;}
// GETTER
std::string getAddr() const{return this->adresse;}
std::vector<People> getHabitant() const{return this->habitant;}
// SETTER
void setAddr(std::string const val){this->adresse = val;}
void addHabitant(People const &p){this->habitant.push_back(p);}
void removeHabitant(People const &p);
// OPERATOR
People & operator[](unsigned int const val){return this->habitant[val];}
};
std::ostream & operator<<(std::ostream & out, Home const &h);
#endif
我希望你对我的问题有任何想法。
ps:对不起,我的英语,对不起,如果我做了任何不好的事,我是新手,作为stackoverflow的帮助者
以及Molbdnilo在一句话中也看来,在std::ostream & operator<<(std::ostream & out, Home const &h)
中,您进行迭代
std::for_each(h.getHabitant().begin(), h.getHabitant().end(), [&out](People const pe){
假定h.getHabitant().begin()
和h.getHabitant().end()
是相同向量的迭代器,但
std::vector<People> getHabitant() const{return this->habitant;}
每次返回vector的新副本。
如果您不想修改 gethabitant 以将const引用返回到居民您必须记住您迭代的向量。
std::vector<People> v = h.getHabitant();
std::for_each(v.begin(), v.end(), [&out](People const pe){
,但我鼓励您修改getHabitant()
为
const std::vector<People> & getHabitant() const {return this->habitant;}
相关文章:
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 如何在 c++ 中'NULL'字符串
- c++使用foreach使数组为null
- 当使用通配符和null指针调用函数时,对输出的说明
- 当字段可以为null时,如何使用C++接口在Avro中写入数据
- 如何在映射中返回null
- 为什么返回 NULL 不会破坏函数?
- 构造函数中的 QQuickItem 父项 null
- 检查字符串是否"null" C++
- fopen 在 gdb 中返回 NULL
- what(): basic_string::_M_construct null not valid
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 为什么要从main()返回NULL?
- 为什么 nlohmann/json 序列化 "null" 而不是在 double 上"0"?
- 当目标指针不是基类的类型时,为什么允许dynamic_cast为多态类生成 null 指针?
- 是否有通用方法可以找到任何以 null 结尾的字符串的长度?
- 为什么TinyXML2的XMLDocument::FirstChild()函数在尝试解析这个有效的XML文件时返回NULL?
- 使用 curl_easy_cleanup(curl) 时收到未经处理的 NULL 异常
- ClassTemplateSpecializationDecl 的定义数据为 null