0

So i have created a class that creates an array of objects from another class and when i try to execute the program and insert new objects to the array i get this error: EXC_BAD_ACCESS(code=EXC_I386_GPFLT) Specifically on the insertion of the 10th element.

this is the class that has the array:

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

the method that inserts class objects to array:

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

here is the class that creates the objects that are being inserted to array:

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

the method that inserts simple strings to the object of that class:

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++;

    }
}
};

And lastly the 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);


  }   

1 Answers1

1

You really, really don't want to store owning pointers to dynamically allocated resources in classes. For a good rundown on why see this answer on what's necessary to store an owning pointer in a class. Using a standard container such as std::vector also greatly simplifies the code as these implement most common operations you may want to do with them, such as insertion, deletion and the like.

For instance, your Savelist class would become the following when using 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); }
};

And similarly for the list_reserve class

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

See also cppreference page on std::vector for an excellent reference on what's available.

Community
  • 1
  • 1
  • Huge thanks man thats really helpful !! How am i going to search for a specific object in saveclass though?? – Dionisis Nikas Apr 27 '17 at 17:42
  • and also on list reserve when i use 'object.insert(0, input);' it pops an error that i am calling a private constructor – Dionisis Nikas Apr 27 '17 at 17:57
  • Ah right, it's `object.insert(object.begin(), input);`, strings allow indexing on numbers but not vectors and I forgot about that. I've fixed the answer. For finding an element you can use `std::find` from `` or write a for loop to iterate through the vector until you find what you're looking for. – Julian Sivertsen Apr 27 '17 at 20:00
  • if you can only answer me one more thing. After i use find to find the selected list_reserve object from the vector, how can i gain access to the methods of this objects class?? – Dionisis Nikas Apr 27 '17 at 21:45