我正在試圖與9個城市直接相連。我遇到了這個功能「短路徑」的問題。我不斷收到一個錯誤「矢量迭代器不兼容」,我無法弄清楚。請任何幫助。由於向量迭代器在C++中不兼容
這是函數:
void City::shortPath(vector<City*> & cities)
{
queue<City*> q;
q.push(this);
for(unsigned int i = 0; i < cities.size(); i++)
{
cities[i]->cityDistance = -1;
cities[i]->visited1 = false;
}
q.front()->visited1 = true;
q.front()->cityDistance = 0;
while(!q.empty())
{
City * v = q.front();
if(v->neighbor1.size() != 0)
{
for(unsigned int i = 0; i < v->neighbor1.size(); i++)
{
City * z = v->neighbor1[i];
q.push(z);
if(z->visited1 == false)
{
v->neighbor1[i]->cityDistance = v->cityDistance + 1;
v->neighbor1[i]->previous1.push_back(*v);
v->neighbor1[i]->visited1 = true;
}
}
}
q.pop();
}
}
這是主要的:
int main()
{
City * NewDelhi = &City("New Delhi");
City * HongKong = &City("Hong Kong");
City * Washington = &City("Washington");
City * Dublin = &City("Dublin");
City * Lisbon = &City("Lisbon");
City * Vienna = &City("Vienna");
City * Santiago = &City("Santiago");
City * RioDeJaneiro = &City("RioDeJaneiro");
City * Berlin = &City("Berlin");
City * NewYork = &City("NewYork");
vector<City*> vector1;
vector1.push_back(HongKong);
vector1.push_back(NewDelhi);
vector1.push_back(Washington);
vector1.push_back(Dublin);
vector1.push_back(Lisbon);
vector1.push_back(Vienna);
vector1.push_back(Santiago);
vector1.push_back(RioDeJaneiro);
vector1.push_back(Berlin);
vector1.push_back(NewYork);
(*HongKong).neighbor1.push_back(NewDelhi);
(*NewDelhi).neighbor1.push_back(Washington);
(*Washington).neighbor1.push_back(Dublin);
(*Dublin).neighbor1.push_back(Lisbon);
(*Lisbon).neighbor1.push_back(Vienna);
(*Vienna).neighbor1.push_back(Santiago);
(*Santiago).neighbor1.push_back(RioDeJaneiro);
(*RioDeJaneiro).neighbor1.push_back(Berlin);
(*Berlin).neighbor1.push_back(NewYork);
(*NewYork).neighbor1.push_back(HongKong);
(*NewYork).shortPath(vector1);
能否請您剪切/粘貼確切的錯誤信息,並指出它的存在就行了? – paulsm4
我要把我的錢放在'v'虛假引用:'v-> neighbor1 [i] - > previous1.push_back(* v);' –
謝謝大家的回覆。我明白了,你是對的菲利普斯先生。謝謝。 – user2419831