错误:当我尝试在类对象数组 C++ 中插入类对象时"EXC_BAD_ACCESS(code=EXC_I386_GPFLT)"

error: "EXC_BAD_ACCESS(code=EXC_I386_GPFLT)" when i try to insert class objects in array of class objects c++

本文关键字:对象 EXC BAD ACCESS GPFLT I386 code 插入 错误 C++ 数组      更新时间:2023-10-16

因此,我创建了一个类,从另一类创建了一个对象数组,当我尝试执行程序并将新对象插入阵列时,我会遇到此错误:exc_bad_access(code = code = =exc_i386_gpflt(特别是插入第10个元素。

这是具有数组的类:

class Savelist{
private:
     list_reserve *reserve;
     list_user *users;
     int length,MaxSize;
     string code;
public:
     string stoixeia;
     Savelist(int MaxListSize)
   {// Constructor for formula-based linear list.
    MaxSize = MaxListSize;
    reserve = new list_reserve (MaxSize, code);
    users=new list_user(MaxSize);
    length = 0;
    }
   ~Savelist() {delete [] reserve,delete [] users;} // destructor
   bool IsEmpty() const {return length == 0;}
   int Length() const {return length;}

将类对象插入数组的方法:

void Insert_flight(list_reserve input)
{// "push" all objects one position up
    if (length!=0)
    {
        for (int i=length-1; i >=0; i--)
        {
            reserve[i]=reserve[i+1];
        }
        reserve[0] = input;
        length++;
                }
    else
    {
        reserve[0] = input;
        length++;
    }
}

这是创建正在插入数组的对象的类:

class list_reserve { 
private:
   bool result;
   int length,MaxSize;
   string *object; // dynamic 1D array
public:
   string code;
   bool get_result;
   // constructor
   list_reserve(int MaxListSize,string flcode)
   {// Constructor for formula-based linear list.
    MaxSize = MaxListSize;
    object = new string [MaxSize];
    length = 0;
    code=flcode;
    }
    ~list_reserve() {delete [] object;} // destructor
    bool IsEmpty() const {return length == 0;}
    int Length() const {return length;}

将简单字符串插入该类的对象的方法:

void Insert_User(string input,string code,list_flight schedule)
{// Insert user
    if (length!=0)
    {
        for (int i=length-1; i >=0; i--)
        {
            object[i+1] = object[i];
        }
        object[0] = input;
        schedule.Get(code);
        schedule.update(schedule.code, 1);
        length++;
    }
    else
    {
        object[0] = input;
        schedule.Get(code);
        schedule.update(schedule.code, 1);
        length++;
    }
}
};

,最后是main((:

   int main(int argc, const char * argv[]) {
       list_reserve  DA001_r(100,"DA001"),DA002_r(100,"DA002"),DA003_r(100,"DA003"),DA004_r(100,"DA004"),DA005_r(100,"DA005")        
,DA006_r(100,"DA006"),DA007_r(100,"DA007"),DA008_r(100,"DA008"),DA009_r(100,"DA009"),DA010_r(100,"DA010"),DA011_r(100,"DA011")
,DA012_r(400,"DA012"),DA013_r(400,"DA013"),DA014_r(100,"DA014"),DA015_r(100,"DA015"),DA016_r(100,"DA016"),DA017_r(100,"DA017")
,DA018_r(100,"DA018"),DA019_r(100,"DA029"),DA020_r(100,"DA020"),DA021_r(100,"DA021"),DA022_r(100,"DA022"),DA023_r(400,"DA023"),
DA024_r(400,"DA024");

Savelist Queues(100);
Queues.Insert_flight(DA001_r);
Queues.Insert_flight(DA002_r);
Queues.Insert_flight(DA003_r);
Queues.Insert_flight(DA004_r);
Queues.Insert_flight(DA005_r);
Queues.Insert_flight(DA006_r);
Queues.Insert_flight(DA007_r);
Queues.Insert_flight(DA008_r);
Queues.Insert_flight(DA009_r);
Queues.Insert_flight(DA010_r);
Queues.Insert_flight(DA011_r);
Queues.Insert_flight(DA012_r);
Queues.Insert_flight(DA013_r);
Queues.Insert_flight(DA014_r);
Queues.Insert_flight(DA015_r);
Queues.Insert_flight(DA016_r);
Queues.Insert_flight(DA017_r);
Queues.Insert_flight(DA018_r);
Queues.Insert_flight(DA019_r);
Queues.Insert_flight(DA020_r);
Queues.Insert_flight(DA021_r);
Queues.Insert_flight(DA022_r);
Queues.Insert_flight(DA023_r);
Queues.Insert_flight(DA024_r);

  }   

您真的,真的不想将拥有的指针存储以在类中动态分配的资源。为了很好地了解为什么在课堂中存储所有指针所需的答案。使用诸如std::vector之类的标准容器也大大简化了代码,因为这些实现了您可能想使用的最常见操作,例如插入,删除等。

例如,使用std::vector

时,您的Savelist类将成为以下内容
class Savelist {
private:
    vector<list_reserve> reserve;
    vector<list_user> users;
    // No need for storing MaxSize or length, std::vector can store any
    // number of elements, and knows how many elements it contains.
    string code;
public:
    string stoixeia;
    // constructor and destructor becomes unnecessary, as the vector member
    // takes care of all the bookkeeping
    bool IsEmpty() const { return reserve.empty(); }
    int Length() const { return reserve.size(); }
    // Insert flight need only pass the input to the vector
    void Insert_flight(list_reserve input) { reserve.insert(reserve.begin(), input); }
};

,类似于list_reserve

class list_reserve { 
private:
   bool result;
   // once again, the vector keeps track of size and elements contained
   vector<string> object;
public:
    string code;
    bool get_result;
    // constructor becomes simpler
    list_reserve(string flcode)
    {
       code = flcode;
    }
    // destructor becomes unnecessary
    bool IsEmpty() const { return object.empty(); }
    int Length() const { return object.size(); }

    void Insert_User(string input, string code, list_flight schedule)
    {
        // note that object.push_back(input); would be
        // more efficient, but inserts the element at the end.
        object.insert(object.begin(), input);
        schedule.Get(code);
        schedule.update(schedule.code, 1);
    }
};

另请参见std::vector上的CPPReference页面,以获取有关可用内容的出色参考。