我正在尝试读取文件并确定要执行的计算类型。我将在类型中读取字符串。稍后我将使用else if (EUC.compare(type) == 0)
进行比较。但它并不总是有效。
这是我的代码:
ifstream infile;
infile.open(fileName.c_str());
// First readin the file that we are to calcuate on
// Define the needed parameters
string word1, word2, word3;
string outtype;
string type = "TBD";
string EUC = "EUC_2D";
// Also want to know what is the dimension we have
int givenDimension;
int dimen = 0;
cout<<endl<<endl;
cout<<"This is "<<fileName<<endl;
cout<<"The information we are given is:"<<endl;
// Read the file and determine the its dimension and type
while(infile >> word1) {
//cout<<word1<<endl;
if (word1 == "DIMENSION") {
infile>> word2 >> givenDimension;
dimen = givenDimension; // Dimensional obtianed
}
if (word1 == "DIMENSION:") {
infile>>givenDimension;
dimen = givenDimension; // Dimensional obtianed
}
if (word1 == "EDGE_WEIGHT_TYPE"){
infile>> word2 >> outtype;
type = outtype;
}
if (word1 == "EDGE_WEIGHT_TYPE:"){
infile>> outtype;
type = outtype;
}
if (word1 == "NODE_COORD_SECTION") {
break;
}
if (word1 == "NODE_COORD_SECTION:") {
break;
}
}
cout<<"Dimenson is "<<dimen<<endl;
cout<<"Type is "<<type<<endl;
cout<<dimen<<endl;
cout<<type<<endl;
// The n*n matrix that contain each city to each city distance
int allcityDistance[(dimen)*(dimen)];
if (type == "GEO") {
// Now we get the data
int x; // Index of city
double y,z; // use to output data
double latitude[dimen]; // Define latitude with the number of dimension
double longitude[dimen]; // Define longitude with the number of dimension
while (infile >> x >> y >> z)
{
// As we obtain y and z as given format
// we convert into latitude and longitude
int index = x-1;
latitude[index] = latitudeAndlongitude(y);
longitude[index] = latitudeAndlongitude(z);
//printf("city %d has %f and %f\n",x,latitude[index],longitude[index]);
}
int check[dimen][dimen];
for (int i = 0; i < dimen; i++) {
//cout<<i<<" ";
for (int j = 0; j < dimen; j++) {
check[i][j] = abs(caldistance(latitude[i], longitude[i], latitude[j], longitude[j]));
allcityDistance[dimen*(i) + j] = check[i][j];
//cout<<check[i][j]<<" ";
}
//cout<<endl;
}
}
else if (EUC.compare(type) == 0){
// Error appear above
// Now we get the data
int x; // Index of city
double y,z; // use to read in data
double xdirection[dimen], ydirection[dimen];
while (infile >> x >>fixed >> y >>fixed >> z)
{
// As we obtain y and z as given format
// we convert into latitude and longitude
//cout<<y<<" "<<z<<endl;
int index = x-1;
xdirection[index] = (y);
ydirection[index] = (z);
//printf("city %d has %f and %f\n",x,xdirection[index],ydirection[index]);
}
double xdifference = 0.0, ydifference = 0.0;
int check[dimen][dimen];
//check[1][1] = 0;
for (int i = 0; i < dimen; i++) {
//cout<<i<<" ";
for (int j = 0; j < dimen; j++) {
if (i == j) {
check[i][i] = 0;
}else{
xdifference = xdirection[i] - xdirection[j];
ydifference = ydirection[i] - ydirection[j];
//cout<<i<<" "<<xdifference<<" "<<ydifference;
check[i][j] = round(sqrt(xdifference*xdifference + ydifference*ydifference));
}
allcityDistance[dimen * i + j] = round(sqrt(xdifference*xdifference + ydifference*ydifference));
//cout<<check[i][j]<<" ";
}
//cout<<endl;
}
}
我一直收到这个错误:
operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
const _CharT* __rhs) _NOEXCEPT{
return __lhs.compare(__rhs) == 0;
}
EXC_BAD_ACCESS
这对我来说真的很奇怪。
答案 0 :(得分:0)
从您的错误日志中,我会说这是导致问题的一个或多个使用==运算符的实例。通常,当您访问未分配的内存区域时,会发生EXC_BAD_ACCESS。查看代码中的所有条件,唯一可能发生错误内存访问的地方是将word1
与各种参数进行比较。
你似乎已经预先初始化了所有内容,所以不应该有糟糕的访问。 word1
是唯一看起来可疑的变量。
您确定从文件中正确读取了吗?文件格式正确吗?