<分区>
我正在编写一个类数据库。
我有一个类对:
class Pair{
MyString m_key;
Data* m_data;
}
然后 AddItem 方法组成对并将其添加到数据库中。
Data Ivanov(1, 10, "Manager", 100000);
Data Petrov(2, 20, "Manager", 200000);
MyString* employee0 = new MyString("Ivanov");
MyString* employee1 = new MyString("Petrov");
bd.AddItem(employee0, &Ivanov);
bd.AddItem(employee1, &Petrov);
数据库本身监控元素的数量和容量。 Bd类{ 私有(private)的: int m_size; int m_capacity; 对** ar; ...
如果我们添加一个项目,如果容量溢出,数据库就会拉伸(stretch)。 如果我们删除一个项目,就会出现一个空单元格,将来可能会用到。 这样做是为了不使堆碎片过多。
void Bd:: AddItem(MyString* key, Data* data){
if (m_size < m_capacity){
*ar[m_size+1] = Pair(key, data);
}
else{
if (m_size == 0){
ar = new Pair*[++m_size];
m_capacity++;
ar[0] = new Pair(key, data);
stop
}
else{
Pair** tmp = new Pair*[++m_size];
m_capacity++;
memcpy(tmp, ar, m_size * sizeof(Pair*));
delete[] ar;
ar = tmp;
ar[m_size-1] = new Pair(key, data);
stop
}
}
}
void Bd::RemoveItem(const char* a_key){
Pair** tmp_ar = this->get_ar();
for (int i = 0; i < m_size; i++){
Pair* tmp_key = tmp_ar[i];
MyString* tmp_my_string= (*tmp_key).get_m_key();
const char* tmp_str = (*tmp_my_string).GetString();
if ( strcmp(tmp_str, a_key) == 0){
delete ar[i];
for (int j = i; j < m_size-1; j++){
*ar[j] = *ar[j+1];
}
delete ar[m_size-1];
m_size--;
break;
}
stop
if (i == (m_size-1)){
cout << "No such person." <<endl;
}
}
stop
}
好吧,现在我想使用 bd。
cout << bd;
bd.Print("Ivanov");
bd.Print("Petrov");
它工作得很好。它打印整个数据库或任何元素。
然后我将 Ivanov 先生移除。 bd.RemoveItem("伊万诺夫");
现在我遇到了这两个问题:
bd.Print("Petrov");
cout << bd;
我在 Data 类中有一个打印方法。
void Data:: print_info(){
cout << "Sex: " << this->getSex() << ", age: " << age <<", position: " << position <<", salary: ";
cout << salary << endl;
}
我特地做了第二次cout,因为这是我遇到麻烦的地方。 薪水虽然在范围内清晰可见,但却导致了整个问题。 请看这张照片:
如果我再执行一步,我会收到有关堆损坏的错误消息。 输出窗口显示:HEAP: Free Heap block 296b30 modified at 296b58 after it was freed
你能帮我理解为什么我不能打印这份工资吗?