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