我一接受输入就调试断言错误。 “删除”有问题吗?

时间:2014-05-23 10:54:29

标签: debugging runtime-error

输入两个元素后,我有调试断言错误。在获取7-8个条目之后,程序给了我访问读取或有时写入违规错误但是在删除动态数组之后,它显示调试断言在取得前两个输入并且发生故障后失败。有任何想法如何解决它?我这里只复制我的航空班。我也有类似的火,土和水类。

错误是BLOCK_TYPE_IS_VALID(pHead-> nBlockUse) 其他人也问过这个问题,但我无法弄清楚我的程序错误。敬请帮助。

#ifndef AIR_H
#define AIR_H
#include <iostream>
#include <string>
#include "element.h"
using namespace std;

class Air :public Element
{
public:
    string air;
    string Elements [2];
    int i;
    string *elements; 

public:
    Air(string n): Element (n)
    {
        air = n;
        i=-1;
        elements = new string[2];
    }


void alreadyExists (string a)
    {
        int lineCount = 0;
        ifstream read;
        read.open ("Air.txt", ios::in | ios::app);
        while(!read.eof())
            {
                string x;
                read>>x;
                lineCount++;

            }

            lineCount--;
            read.close();
            read.open("Air.txt", ios::in|ios::app);
            for(int i = 0; i < lineCount; i++)
            {
                read>>elements[i];
            }
            bool Found = false;
            for(int i = 0; i < lineCount; i++) {
                if(a == elements[i]) 
                {
                    Found = true;
                    break;
                }
            }
            if(!Found) 
            {
                write2file (a);

            }
    }


void write2file (string air)
{
    ofstream write;
    write.open ("Air.txt", ios::out|ios::app);
    {
        write<<air<<endl;

    }

}


     void DisplayA ()
     {
         /*for(int i=0; i<2; i++)//( to read through the arrays )
         {
             cout<<Elements[i]<<endl;
         }*/
         ifstream read ("Air.txt", ios::in|ios::app);

         int i=0;
         while (read >> Elements[i])
                 {
                     cout<<Elements[i]<<endl;
                 }

     }

     Air operator+(Air & air) 
    {                                         
        Air newElement ("NULL");        
        if (this->air == "Air"||this->air=="air"&& air.air == "Air"||air.air=="air")
        {
            newElement.air = "Pressure";
            cout<<"Yay!! You've made: "<<newElement.air<<endl;
            alreadyExists (newElement.air);

            //PushA (newElement.air);
            //write2file (newElement.air);
            return newElement;
        }

        else if ((this->air == "Air"||this->air == "air") && (air.air == "Pressure"||air.air=="pressure"))/* || ((this->air == "Pressure"||this->air=="pressure") && ( air.air == "Air"||air.air=="air")))*/
        {
            newElement.air = "Atmosphere";
            cout<<"Wuhooo!! You've made: "<<newElement.air<<endl;
            alreadyExists (newElement.air);
            //PushA (newElement.air);
            //write2file (newElement.air);
            return newElement;
        }

        else return newElement;

     }//pressure, atmosphere

    ~ Air ()
    {
        delete []elements;
    }

};
#endif

1 个答案:

答案 0 :(得分:0)

 BLOCK_TYPE_IS_VALID (pHead->nBlockUse)

断言表示删除/清除时损坏的堆。

据我所知,你错过了Element的虚拟解构器。 尝试类似:

virtual ~Element() {}
在类Element中

。 请发布Element类。

祝你好运!