我在一个类中有以下Vertex
结构:Graph
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;