for_each 算法导致 basic_string::_M_construct null 在 C++ 中无效

for_each algorithm causes basic_string::_M_construct null not valid in C++

在学习C++的项目中,我创建了一个由两个easy组成的软件class (家和人)。 人们有构造函数:

// 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));
}

这里是"operator<<"的代码:

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

经过多次研究,我认为这是home的原因class,所以这是home.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:抱歉我的英语不好,如果我有什么不好的地方,我很抱歉我是 Whosebug 的新求助者

正如 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;}

returns 每次向量的新副本

如果您不想将 getHabitant 修改为 return 对 habitant 的常量引用,您必须记住向量你迭代的地方。

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