מחיקה של אובייקט במערך בC++ גורמת לתעופה - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

מחיקה של אובייקט במערך בC++ גורמת לתעופה


SweeT_EviL

Recommended Posts

היי,

אני מנסה למחוק אובייקט במערך של אובייקטים מסוג מסויים והדבר גורם לתעופה.

בעיקרון מה שאני רוצה הוא רק לגרום לDTOR שלו להיקרא כי להשוואות אותו לNULL זאת לא בעיה...

הנה חלקים ממה שכתבתי:

student.h

#ifndef _STUDENT
#define _STUDENT

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Student{
private:
string _name;
int _id;
vector<int> _grades;
float _avg;

static int maxGrade;
static int gradesCount[100];

static const int MAX_NAME_LENGTH = 20;
static const int MIN_ID_LENGTH = 10000;
static const int MAX_ID_LENGTH = 99999;
static const int PASS_GRADE = 60;
static const int MIN_VALID_GRADE = 1;
static const int MAX_VALID_GRADE = 100;
public:
/*
Student Ctor that sets the student's id, name and avg.
*/
Student(string name, int id)
{
if(!name.empty() && name.length() <= MAX_NAME_LENGTH && id >= MIN_ID_LENGTH && id< MAX_ID_LENGTH)
{
_id = id;
_name = name;
}
else
{
_id = MAX_ID_LENGTH;
_name = "none";
}
_avg = 0;
}

/*
Student copy ctor.
*/
Student(const Student& stud)
{
_id = stud._id;
_name = stud._name;
_avg = stud._avg;

//activate the copy ctor of vector
_grades = stud._grades;

unsigned int i;
for(i=0 ; i< _grades.size() ; ++i)
{
++gradesCount[_grades[i]];
}
}

/*
Student Dtor that delete the grades array.
*/
~Student()
{
unsigned int i;
for(i=0 ; i < _grades.size() ; ++i)
{
--gradesCount[_grades[i]];
}

bool maxFound = false;
for(i = MAX_VALID_GRADE ; i >= MIN_VALID_GRADE ; --i)
{
if(gradesCount[i] > 0)
{
maxGrade=i;
maxFound = true;
break;
}
}

if(!maxFound)
{
maxGrade = 0;
}

_grades.clear();
}

int getId() const;
string getName() const;
void addGrade(int grade);
void removeGrade(int grade);
};

#endif

grades.h

#ifndef _GRADES
#define _GRADES

#include "Student.h"

using namespace std;

class Grades{
private:
Student** _students;
int _arraySize;
int _freeCells;

static const int ARRAY_SIZE_INTERVAL = 10;

/*
Enlarge the array
*/
void enlargeArray();
public:

/*
Grades default ctor - init 2 students into the students array.
*/
Grades()
{

_arraySize=ARRAY_SIZE_INTERVAL;
_students = new Student*[_arraySize];
_students[0] = new Student("auto1",12345);
_students[1] = new Student("auto2",67890);

int i;
for (i=2; i < _arraySize ; ++i)
{
_students[i] = NULL;
}

_freeCells = _arraySize - 2;
}

/*
Grades copy ctor.
*/
Grades(const Grades& grade)
{
_arraySize = grade._arraySize;
_students = new Student*[_arraySize];

int i;
for(i=0 ; i<_arraySize ; ++i)
{
_students[i] = new Student(*grade._students[i]);
}

_freeCells = grade._freeCells;
}

/*
Grades dtor.
*/
~Grades()
{
int i;
for(i=0 ; i<_arraySize ; ++i)
{
delete[]_students[i];
}
}

int findStudent(int id) const;
Student* getStudent(int id) const;
void addStudent(string name, int id);
void removeStudent(int id1);
};

#endif

grades.cpp

#include "Grades.h"
void Grades::removeStudent(int id1)
{
int index1 = findStudent(id1);
if(index1 != -1)
{
delete[]_students[index1];
_students[index1] = NULL;
--_freeCells;

if(!(index1 == _arraySize-1))
{
int i;
for(i=index1+1; i < _arraySize - _freeCells; ++i)
{
_students[i-1] = _students[i];
}

_students[i-1] = NULL;
}
}
}

/*
Enlarge the array
*/
void Grades::enlargeArray()
{
Student**tmp = new Student*[_arraySize + ARRAY_SIZE_INTERVAL];

int i;
for(i=0 ; i < _arraySize ; ++i)
{
tmp[i] = _students[i];
}

_arraySize+=ARRAY_SIZE_INTERVAL;

for(i=_arraySize; i < _arraySize ; ++i)
{
tmp[i] = NULL;
}

_students = tmp;
_freeCells = ARRAY_SIZE_INTERVAL;
}

ולאחר שאני מוסיף כמה איברים בMAIN אני מנסה לעשות

grd.removeStudent(12345);

והוא זורק לי

_BLOCK_TYPE_IS_VALID(pHead -> nBlockUse)

זה קורה כשהוא מגיע לפונקציה removeStudent לשורה עם הdelete.

מה לא בסדר עם הDELETE הזה?

תודה.

עריכה:

עכשיו אני שמתי לב שגם אם אני שם את השורה של הremoveStudent בהערה ומגיע לסוף התוכנית בMAIN, אז מחכה לי הכנסת קלט כדי שהמסך לא יעלם.

ושמה לאחר שאני מכניס אות ונותן אנטר הוא שוב זורק לי את השגיאה הזו...

הקוד של סוף הMAIN נראה ככה:


char a;
cin >> a;

קישור לתוכן
שתף באתרים אחרים

מצאתי את הבעיה.

הקטע הוא שעושים DELETE לפי איך שעושים NEW.

למה הכוונה, אם עושים

new t[]

לאובייקט אז כדי למחוק אותו עושים

delete[]

אבל אם יוצרים אובייקט חדש ככה

new t

אז עושים רק delete

ומכיוון שאני רוצה למחוק רק איבר אחד ולא את כל המערך אז delete לאותו איבר מספיק. אם תסתכלו על הDTOR שלי מופיעה שם אותה בעיה ולכן עפה לי השגיאה גם כשהתוכנית מסתיימת.

יום טוב.

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...