私には人を追跡する割り当てがあり、今のところ私はクラッド操作を行う必要があります。動的配列にアクセスしようとすると、人のIDのゲッターは機能しますが、人の電話のゲッターは「セグメンテーション違反」を返し、人の名前のゲッターは何も表示しません。
//the main.cpp test that gives the following error
Controller ctrl(repp,repa,valp,vala);
ctrl.addPerson(1,"Name","0744000000","Adress");
ctrl.show();
//controller show method, repp - instance of repository in controler
void Controller::show()
{
repp->show();
}
//repository show method, which doesn't work
void PersonInMemoryRepository::show()
{
for(int i=0; i < pers.getSize(); i++)
cout<<pers.get(i)->getName()<<endl;
}
//getById method in repository
const Person* PersonInMemoryRepository::getById(int id)
{
for (int i = 0; i < pers.getSize(); i++)
{
if (pers.get(i)->getId() == id) {
return pers.get(i);
}
}
return NULL;
}
//the Person class
class Person
{
public:
Person(int i, string n, string p, string a);
const string& getName() const {
return name;
}
const string& getPhone() const {
return phone;
}
const string& getAdress() const {
return adress;
}
int getId() const {
return id;
}
~Person();
private:
int id;
string name;
string phone;
string adress;
};
//pers.get(i)
template<typename Element>
Element DynamicArray<Element>::get(int poz) {
return elems[poz];
}
前もって感謝します。
更新:行にある場合cout <getName(); 。pers.get(i)は適切に機能し、Person *タイプのベクトルであり、デバッガーで逆参照すると正しい値になりますが、-> getName()で「無効な繰り返し数0」と表示されます。