運行以下代碼時出現的錯誤是「Debug As thesis Failed ... Expression _BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)」。當在C中使用char *時出現「調試斷言失敗」錯誤
我的readTXT方法需要傳遞一個char *對象,但我想讓用戶選擇爲自己輸入值。
char * mapName;
int main()
{
//load map
int mapSelection;
cout << "select a map";
cin >> mapSelection;
switch (mapSelection)
{
case 1:
mapName = "walls1.txt";
break;
case 2:
mapName = "walls2.txt";
break;
case 3:
mapName = "maze1.txt";
break;
case 4:
mapName = "maze2.txt";
break;
}
map = readTXT(mapName, 8, 11);
delete mapName;
...
這是readTXT方法
double* readTXT(char *fileName, int sizeR, int sizeC)
{
double* data = new double[sizeR*sizeC];
int i=0;
ifstream myfile (fileName);
if (myfile.is_open())
{
while (myfile.good())
{
if (i>sizeR*sizeC-1) break;
myfile >> *(data+i);
cout << *(data+i) << ' '; // This line display the converted data on the screen, you may comment it out.
if (i == 10 || i == 21 || i == 32 || i == 43 || i == 54 || i == 65 || i == 76)
{
cout << "\n";
}
i++;
}
myfile.close();
}
else cout << "Unable to open file";
//cout << i;
return data;
}
不要'刪除'你沒有'新'的內存。 – chris 2013-04-04 18:05:48