unhandled exception in xxx.exe 0xC0000005:Access Violation
麻烦大家一下:
当我的程序运行到倒数第九行时(即:face_Info Info_temp(0,temp_name,xMin,xMax);face_Info是我自己定义的一个类)总跳出一个对话框说:unhandled exception in xxx.exe 0xC0000005:Access Violation,在网上查了很多资料,可是都没有找到相近的例子可以参考,故发帖。程序如下:
//////定义vector<face_Info> all_face_Info, 来存放所有面的标号, 虚实信息和xMin xMax
vector<face_Info> all_face_Info;
/////下面是从文件face.trn中读出所有面的信息, 将其存放到vector<face_Info> all_face_Info中
pFile1 = fopen("D:\\program\\face.trn", "r");
if (!pFile1)
{
cout << "cannot open the file";
}
while (!feof(pFile1))
{
face_Info Info_temp;
int temp_name=0;
double xMin=0;
double xMax=0;
char strLine[255];
fgets(strLine, 255, pFile1); //读取每一行
//判断是否头标志
if ( sscanf(strLine, "Face: face.%d", &temp_name) != 0 )
{
while(!feof(pFile1))
{
fgets(strLine, 255, pFile1);
int totaledge=0;
if (sscanf(strLine, " Total Edges: %d", &totaledge) != 0 )
{
fgets(strLine, 255, pFile1);
fgets(strLine, 255, pFile1);
totaledge=totaledge*2; //一条线有两个点
//vertex_num用来存放nface所有的点的标号, 并且同一个标号在vertex_num里出现了两次
vector<int> vertex_num(totaledge,0);
for (std::vector<int>::size_type ix=0; ix!=vertex_num.size(); ix=ix+2)
{
fgets(strLine, 255, pFile1);
if ( sscanf(strLine, " %*s No vertex.%d %*s vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No v_vertex.%d %*s vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No vertex.%d %*s v_vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No v_vertex.%d %*s v_vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else{}
}
///排序
std::sort(vertex_num.begin(),vertex_num.end());
///删除重复
std::vector<int>::iterator end_unique = std::unique(vertex_num.begin(), vertex_num.end());
vertex_num.erase(end_unique, vertex_num.end());
//用个容器存放所有nface所有点的x坐标值
vector<double> xcoord_of_nface;/////最终, vector<double> xcoord_of_nface 和 vector<int> vertex_num大小相等了
for( std::vector<int>::size_type iy=0; iy!=vertex_num.size(); ++iy)
{
for( std::vector<vertex_Info>::iterator iter=all_vertex_Info.begin(); iter!=all_vertex_Info.end(); ++iter )
{
if( (*iter).name() == vertex_num[iy])
xcoord_of_nface.push_back((*iter).xcoord());
else{}
}
}
std::stable_sort(xcoord_of_nface.begin(), xcoord_of_nface.end(), isLess);
face_Info Info_temp( 1, temp_name, xcoord_of_nface.front(), xcoord_of_nface.back() );
all_face_Info.push_back(Info_temp);
break;
}
}
}
else if ( sscanf(strLine, "Face: v_face.%d", &temp_name) != 0)
{
while(!feof(pFile1))
{
fgets(strLine, 255, pFile1);
int totaledge=0;
if (sscanf(strLine, " Total Edges: %d", &totaledge) != 0 )
{
fgets(strLine, 255, pFile1);
fgets(strLine, 255, pFile1);
totaledge=totaledge*2; //一条线有两个点
//vertex_num用来存放nface所有的点的标号, 并且同一个标号在vertex_num里出现了两次
vector<int> vertex_num(totaledge,0);
for (std::vector<int>::size_type ix=0; ix!=vertex_num.size(); ix=ix+2)
{
fgets(strLine, 255, pFile1);
if ( sscanf(strLine, " %*s No vertex.%d %*s vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No v_vertex.%d %*s vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No vertex.%d %*s v_vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else if ( sscanf(strLine, " %*s No v_vertex.%d %*s v_vertex.%d %*s ", &vertex_num[ix], &vertex_num[ix+1]) == 0)
{}
else{}
}
///排序
std::sort(vertex_num.begin(),vertex_num.end());
///删除重复
std::vector<int>::iterator end_unique = std::unique(vertex_num.begin(), vertex_num.end());
vertex_num.erase(end_unique, vertex_num.end());
//用个容器存放所有nface所有点的x坐标值
vector<double> xcoord_of_nface;/////最终, vector<double> xcoord_of_nface 和 vector<int> vertex_num大小相等了
for( std::vector<int>::size_type iy=0; iy!=vertex_num.size(); ++iy)
{
for( std::vector<vertex_Info>::iterator iter=all_vertex_Info.begin(); iter!=all_vertex_Info.end(); ++iter )
{
if( (*iter).name() == vertex_num[iy])
xcoord_of_nface.push_back((*iter).xcoord());
else{}
}
}
std::stable_sort(xcoord_of_nface.begin(), xcoord_of_nface.end(), isLess);
face_Info Info_temp( 0, temp_name, xcoord_of_nface.front(), xcoord_of_nface.back() );
all_face_Info.push_back(Info_temp);
break;
}
}
}
else{}
}
fclose(pFile1);
这个问题让我头痛了很久
好像是class的定义出了问题,可我就是找不出问题出在哪里,唉,真是恼火
class的定义如下:
/////定义类face_Info, 用来存放face的信息
class face_Info
{
public:
face_Info(): real_or_virtual(0), name_number(0) {}
face_Info(const bool temp_real_or_virtual, const int temp_name_number): real_or_virtual(temp_real_or_virtual), name_number(temp_name_number), xcoord_Min(0.0), xcoord_Max(0.0){}
face_Info(const bool temp_real_or_virtual, const int temp_name_number, const double temp_xcoord_Min, const double temp_xcoord_Max): real_or_virtual(temp_real_or_virtual), name_number(temp_name_number), xcoord_Min(temp_xcoord_Min), xcoord_Max(temp_xcoord_Max){}
bool r_or_v() const { return real_or_virtual; }
int name() const { return name_number; }
double xMin() const { return xcoord_Min; }
double xMax() const { return xcoord_Max; }
face_Info& operator=( const face_Info & temp )
{
this->real_or_virtual=temp.real_or_virtual;
this->name_number=temp.name_number;
this->xcoord_Min=temp.xcoord_Min;
this->xcoord_Max=temp.xcoord_Max;
return *this;
}
private:
bool real_or_virtual;
int name_number;
double xcoord_Min;
double xcoord_Max;
friend ostream& operator <<(ostream& out, const face_Info & s);
};
ostream& operator <<(ostream & out,const face_Info & s)
{
out<<s.real_or_virtual<<"\t"<<( s.real_or_virtual == 1 ? "face." : "v_face." )<<setprecision(6)<<s.name_number<<"\t"<<setprecision(10)<<s.xcoord_Min<<"\t"<<setprecision(10)<<s.xcoord_Max<<"\t";
return out;
}
有一次出现了一个相同的问题出,同样是unhandled exception in xxx.exe 0xC0000005:Access Violation,但错误出现在class的定义里,在这行:
bool r_or_v() const { return real_or_virtual; }
可到底是哪里错了,我连哪里错了都不知道,让我怎么从几千行的程序里找啊