问题在于 删除x 。在下面的示例中,我创建了一个带有constructor
的类,该类将带有整数值的文件加载到array of x
中。和朋友的功能输出文件,那些工作正常,但问题是deconstructor
,我得到了这个错误,并尝试搜索解决方案,但没有用。
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;
}