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

时间:2019-02-20 11:48:52

标签: c++ for-loop stdstring

在学习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));
}

这里是“ 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类的原因,所以这是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:对不起,我的英语,对我的不便之处,我感到抱歉,我是StackOverflow中的新求职者

1 个答案:

答案 0 :(得分:3)

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

每次返回向量的新副本

如果不想修改 getHabitant 以返回对 habitant 的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;}