我有一個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