抱歉,我在 C++ 方面相对较新,所以我正在调试这段代码,这在第三次调用 cout << 时给了我访问冲突(就在 load_array 返回之后):
#include <cstdlib>
#include <iostream>
#include <fstream>
#define FILENAME "H:\\test.preisach"
#define ROWS 32760
#define COLUMNS 4
using namespace std;
class cPreisach {
public:
float input;
float pdf[ROWS][COLUMNS];
void load_array (){
char c;
float value=0;
int getnum=0, rowno=0, columnno=0, power=0;
bool decimalpoint=false, exponential=false;
ifstream pdf_file (FILENAME);
if (pdf_file.is_open()) {
while (pdf_file.good()) {
c=pdf_file.get();
switch (c) {
case '0': if (exponential == false) {value = (value*10); if (decimalpoint=true) getnum++;} else power=power*10; break;
case '1': if (exponential == false) {value = (value*10)+1; if (decimalpoint=true) getnum++;} else power=(power*10)+1; break;
case '2': if (exponential == false) {value = (value*10)+2; if (decimalpoint=true) getnum++;} else power=(power*10)+2; break;
case '3': if (exponential == false) {value = (value*10)+3; if (decimalpoint=true) getnum++;} else power=(power*10)+3; break;
case '4': if (exponential == false) {value = (value*10)+4; if (decimalpoint=true) getnum++;} else power=(power*10)+4; break;
case '5': if (exponential == false) {value = (value*10)+5; if (decimalpoint=true) getnum++;} else power=(power*10)+5; break;
case '6': if (exponential == false) {value = (value*10)+6; if (decimalpoint=true) getnum++;} else power=(power*10)+6; break;
case '7': if (exponential == false) {value = (value*10)+7; if (decimalpoint=true) getnum++;} else power=(power*10)+7; break;
case '8': if (exponential == false) {value = (value*10)+8; if (decimalpoint=true) getnum++;} else power=(power*10)+8; break;
case '9': if (exponential == false) {value = (value*10)+9; if (decimalpoint=true) getnum++;} else power=(power*10)+9; break;
case '.': decimalpoint=true; break;
case 'E': exponential = true; break;
case ' ': if (value != 0){
value = value/(10^(getnum-power)); /*cout << rowno << columnno << "\n";*/ pdf[rowno][columnno] = value; value=0; power=0; getnum=0; decimalpoint=false;
exponential=false; columnno++;
} break;
case '\n': value = value/(10^(getnum-power)); /*cout << "newline" << rowno << columnno << "\n";*/ pdf[rowno][columnno] = value; value=0; power=0; getnum=0; decimalpoint=false;
exponential=false; rowno++; columnno=0; break;
}
}
pdf_file.close();
}
}
void do_calculation (float input) {}
};
int main () {
float input;
cout << "welcome to the preisach function \n";
cPreisach Preisach;
cout << "created object, loading array \n";
Preisach.load_array();
cout << "please input magnetic field strength \n";
cin >> input;
Preisach.do_calculation (input);
return 0;
}
我不知道为什么 cout 会导致访问冲突,但它一定与数组有关,因为如果该函数被注释掉,cout 就可以了。
谢谢
编辑,因为我还不允许回答我自己的问题:这确实是缓冲区溢出,部分原因是我忘记从 [0][0] 而不是 [1][1] 递增。不知道为什么我没有想到;相当愚蠢。谢谢大家:)