Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调试断言失败。\u块\u类型\u有效_C++ - Fatal编程技术网

C++ 调试断言失败。\u块\u类型\u有效

C++ 调试断言失败。\u块\u类型\u有效,c++,C++,问题在于删除x。在下面的示例中,我创建了一个带有构造函数的类,该构造函数将一个带有整数值的文件加载到一个x数组中。还有一个输出文件的friend函数,这些函数可以正常工作,但问题是解构器出现了问题,我遇到了错误并尝试搜索解决方案,但没有用 class MyFile{ private: int *x; int size; FILE *file; public: MyFile(char *); MyFi

问题在于删除x。在下面的示例中,我创建了一个带有
构造函数的类,该构造函数将一个带有整数值的文件加载到一个x
数组中。还有一个输出文件的friend函数,这些函数可以正常工作,但问题是解构器出现了问题,我遇到了错误并尝试搜索解决方案,但没有用

 class MyFile{
    private:
        int *x;
        int size;
        FILE *file;
    public:
        MyFile(char *);
        MyFile();
        ~MyFile();
        friend ostream& operator<<(ostream &co, MyFile fi);

    };


    MyFile::~MyFile()
    {
        delete x;
            fclose(file);

    }
    MyFile::MyFile(char *Loc_Name)
    {
        file = fopen(Loc_Name, "r");
        int counter = 0, reader,i=0;
        if (file == NULL){ cout << "the file doesnt exists"; exit(0); }
        else
        {
            while (!feof(file))
            {
                fscanf(file, "%d", &reader);
                counter++;

            }
            size = counter;  // the size of array equals counter
            x = new int[size];
            fseek(file, 0, SEEK_SET); // to return file to set otherwise it wont get inside the loop 
            while (!feof(file))
            {
                fscanf(file, "%d", &reader);
                x[i] = reader;
                i++;
            }



        }



    }
    ostream& operator<<(ostream &co, MyFile fi)
    {
        for (int i = 0; i < te.size; i++)
        {
            co << "the elements = " << te.x[i] << endl;
        }
        return co;
    }
    int main()
    {
        MyFile a("d://here.txt"); 
        cout << a;
            return 0;
    }
类MyFile{
私人:
int*x;
整数大小;
文件*文件;
公众:
我的文件(字符*);
MyFile();
~MyFile();

friend ostream&Operator更改为:
delete[]x;
它尝试了,但得到了相同的错误。@arash该文件存在吗?打开是否成功。因为如果不存在,则不会调用
x
上的
new
。您需要在windows中使用反斜杠
D:\\here.txt
该文件确实存在,并且一切正常。它甚至会输出数据,但不知道为什么它不能释放x from内存。谢谢你建议使用反斜杠。@Arash