C++ 为什么在获取此变量时会出现分段错误?

C++ 为什么在获取此变量时会出现分段错误?,c++,segmentation-fault,C++,Segmentation Fault,我从文件中提取字符串形式的名称,创建一个Person*p对象,并将其放入数组中。 然后在数组中搜索一个名称,但当我试图获取该名称时,会出现分段错误 为什么会发生这种分割错误,我可以做些什么来修复它 #include <iostream> #include <string> using namespace std; class Person { private: string firstName; string lastName; string

我从文件中提取字符串形式的名称,创建一个
Person*p
对象,并将其放入数组中。 然后在数组中搜索一个名称,但当我试图获取该名称时,会出现分段错误

为什么会发生这种分割错误,我可以做些什么来修复它

#include <iostream>
#include <string>

using namespace std;

class Person {

private:
    string firstName;
    string lastName;
    string phoneNumber;

public:
    Person();
    Person(string f, string l, string n);
    ~Person(void);

    void setName()
    {
    }
    string getFirstName()
    {
        return firstName;
    }
    string getLastName()
    {
        return lastName;
    }

    string getNumber() { return phoneNumber; }

    void print();
};
#包括
#包括
使用名称空间std;
班主任{
私人:
字符串名;
字符串lastName;
字符串电话号码;
公众:
人();
Person(字符串f、字符串l、字符串n);
~人(无效);
void setName()
{
}
字符串getFirstName()
{
返回名字;
}
字符串getLastName()
{
返回姓氏;
}
字符串getNumber(){return phoneNumber;}
作废打印();
};
数组创建

{
            ifstream file;

            file.open("phonebook.txt");

            if (!file.is_open()) //Check for File Error.
            {
                cerr << "Failed to open file" << endl;
                exit(1);
            }

            //Get Array Size
            string line;
            while (getline(file, line))
            {
                count++;
            }
            file.close();

            //Create an array
            Person *arrList[count];
            buildArray(arrList, count);

        if (uInput == "a" || uInput == "A") //To add
        {
            int x = addPerson();
            if (x == 1)
            {
                count++;
            }
            delete[] arrList;
        }


void buildArray(Person *arr[], int size)
{
    string f, l, n;
    ifstream file;
    file.open("phonebook.txt");
    for (int i = 0; i < size; i++)
    {
        file >> f >> l >> n;
        Person *p = new Person(f, l, n);
        arr[i] = p;
        delete p;
    }
}
{
ifstream文件;
打开(“phonebook.txt”);
如果(!file.is_open())//检查文件错误。
{
cerr f>>l>>n;
人员*p=新人员(f、l、n);
arr[i]=p;
删除p;
}
}
搜索,这是有麻烦的部分。我尝试了一些不同的方法,包括创建两个人,比较他们的角色,但每当它进入.h时,它就不能返回名称

if (uInput == "s" || uInput == "S")
            {                   //To Search
                string f, l;

                cout << "Find Who (Firstname Lastname) " << endl;
                cin >> f >> l;
                Person *n = new Person(f, l, "");
                int i = 0;
                bool found = false;

                while (i <= count && found == false)
                {
                    Person *p = new Person("", "", "");
                    p = arrList[i];

                    if (p->getFirstName() == n->getFirstName() && p->getLastName() == n->getLastName())
                    {
                        arrList[i]->print();
                        found = true;
                        delete p;
                        delete n;
                    }
                    i++;
                }
                while (i == count && found == false)
                {
                    cout << "No results found. " << endl;
                    found = true;
                }
            }
if(uInput==“s”| uInput==“s”)
{//搜索
字符串f,l;
cout f>>l;
人员*n=新人员(f,l,“”);
int i=0;
bool-found=false;
而(i getFirstName()==n->getFirstName()&&p->getLastName()==n->getLastName())
{
arrList[i]->print();
发现=真;
删除p;
删除n;
}
i++;
}
while(i==count&&found==false)
{

无法编辑您的问题以添加代码,以及您为解决此问题所做的尝试。一旦您添加了问题,我将投票重新打开。真的吗?大部分图像转储现在被阻止了?这是我听过的最好消息。早该更改了。@JamesStark如果不确定如何正确格式化代码,请将其粘贴为文本,我们将对其进行编辑以格式化很适合你。好的,谢谢@πάνταῥεῖ 对于旅游建议,我会做的,AsopPo.OK,我想好如何格式化它。谢谢。我想在做任何与动态内存分配有关的工作之前,都需要花费足够的时间来解释C++如何在这方面工作,这是基本的基础。在那之前,解释C++的内存模型需要花费足够多的钱:什么指针?它们是如何工作的,等等。。。