我有一个Graph
类以下Vertex
结构:C++“访问冲突读取位置”错误
struct Vertex
{
string country;
string city;
double lon;
double lat;
vector<edge> *adj;
Vertex(string country, string city, double lon, double lat)
{
this->country = country;
this->city = city;
this->lon = lon;
this->lat = lat;
this->adj = new vector<edge>();
}
};
当调用我写所谓getCost()
的方法,我不断收到同样的未处理的异常
访问冲突读取位置0x00000048
,我想不通为什么。
getCost()
的方法:
void Graph::getCost(string from, string to)
{
Vertex *f = (findvertex(from));
vector<edge> *v = f->adj; // Here is where it gives the error
vector<edge>::iterator itr = v->begin();
for (; itr != v->end(); itr++)
{
if (((*itr).dest)->city == to)
cout << "\nCost:-" << (*itr).cost;
}
}
findvertex()
返回Vertex*
类型的值的方法。为什么我一直收到这个错误?
的findVertex方法:
Vertex* Graph::findvertex(string s)
{
vmap::iterator itr = map1.begin();
while (itr != map1.end())
{
if (itr->first == s){
return itr->second;
}
itr++;
}
return NULL;
}
凡map1
定义:
typedef map< string, Vertex *, less<string> > vmap;
vmap map1;
您已将所有内容贴在最相关的部分旁边。提示:findvertex – SomeWittyUsername
你可以发布findVertex吗? – Alan
什么是findvertex?即如果它不返回具体的顶点对象,那就是你的错误。 – Chris