C++ 错误:";EXC“坏”访问(代码=EXC“I386”GPFLT)“;当我尝试在类对象数组c++;

C++ 错误:";EXC“坏”访问(代码=EXC“I386”GPFLT)“;当我尝试在类对象数组c++;,c++,arrays,C++,Arrays,因此,我创建了一个类,该类从另一个类创建了一个对象数组,当我尝试执行该程序并向数组中插入新对象时,会出现以下错误:EXC_BAD_ACCESS(code=EXC_I386_GPFLT) 特别是第10个元素的插入 这是具有数组的类: class Savelist{ private: list_reserve *reserve; list_user *users; int length,MaxSize; string code; public: st

因此,我创建了一个类,该类从另一个类创建了一个对象数组,当我尝试执行该程序并向数组中插入新对象时,会出现以下错误:EXC_BAD_ACCESS(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_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++;

    }
}
};
最后是主要的()


您确实不想在类中存储指向动态分配资源的指针。要详细了解为什么要看。使用标准容器(如
std::vector
)也大大简化了代码,因为这些容器实现了您可能希望使用它们执行的最常见操作,如插入、删除等

例如,当使用
std::vector

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); }
};

另请参阅,以获取有关可用内容的优秀参考。

使用调试器。请注意,不建议使用原始指针新建和删除。使用
std::vector
而不是动态分配的数组可能会解决您的大多数问题。如果我根本不使用析构函数,这是否很糟糕???@DionisisNikas是的。有人能发布一个我将使用“std::vector”的代码示例吗??因为我希望能够搜索向量并根据它们的内容获得我想要的类对象,然后使用所选对象的方法。非常感谢,这真的很有帮助!!但是,当我使用“object.insert(0,input);”时,如何在saveclass中搜索特定对象,以及在list reserve中搜索特定对象它弹出一个错误,我正在调用一个私有构造函数。对,它是
object.insert(object.begin(),input),字符串允许对数字进行索引,但不允许对向量进行索引,我忘记了这一点。我已经确定了答案。要查找元素,您可以使用
std::find
from
或编写For循环来迭代向量,直到找到您要查找的内容。如果您只能回答我一件事的话。使用“查找”从向量中查找选定的列表_reserve对象后,如何访问此对象类的方法??