gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:10:23 33 4
gpt4 key购买 nike

所以我创建了一个类,该类从另一个类创建对象数组,当我尝试执行程序并将新对象插入数组时,我收到此错误: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_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);


}

最佳答案

您真的、真的不想在类中存储指向动态分配资源的拥有指针。有关原因的详细说明,请参阅 this answer on what's necessary to store an owning pointer in a class .使用诸如 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);
}
};

另见 cppreference page on std::vector以获得有关可用内容的极好引用。

关于c++ - 错误 : "EXC_BAD_ACCESS(code=EXC_I386_GPFLT)" when i try to insert class objects in array of class objects c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43658103/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com