简体   繁体   English

向量迭代器在C ++中不兼容

[英]Vector iterators incompatible in c++

I am trying to do a graph with 9 cities directly connected. 我正在尝试绘制直接连接9个城市的图表。 I am having this problem with the function "shortpath". 我在函数“ shortpath”中遇到了这个问题。 I keep getting an error "vector iterators incompatible" and I can't figure it out. 我不断收到错误“向量迭代器不兼容”的信息,我无法弄清楚。 Any help please. 请帮忙。 Thanks 谢谢

This is the function: 这是功能:

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();

}




}

And this is the main: 这是主要的:

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 );

As Zan Lynx mentioned up there... you're using bad pointers. 正如Zan Lynx在上面提到的那样……您使用的是错误的指针。

I did write a quick test and there is the output of g++ 我确实写了一个快速测试,有g ++的输出

make a
g++     a.cpp   -o a
a.cpp: In function ‘int main()’:
a.cpp:15:35: error: taking address of temporary [-fpermissive]
make: *** [a] Error 1

As you can see, it tells me that taking the address of a temporary is bad. 如您所见,它告诉我使用临时地址是不好的。 Not only that, it does not actually compile since that's an error. 不仅如此,它实际上并不能编译,因为那是一个错误。 cl could definitively do better on that one! cl绝对可以在那一个上做得更好!

My test code, just in case: 我的测试代码,以防万一:

#include <iostream>

class City
{
public:
  City(const std::string& name) : name_(name) { std::cout << "Constructed" << std::endl; }
  ~City() { std::cout << "Destructed" << std::endl; }
private:
  const std::string name_;
};

int main()
{
  std::cout << "Started" << std::endl;
  City *SF = &City("San Francisco");
  char c;
  std::cin >> c;
}

This can happen when you try to use an iterator that belongs to another vector of the same type. 当您尝试使用属于另一个相同类型向量的迭代器时,可能会发生这种情况。 At some point you may be making a copy of a vector but and you use iterators from the original vector with the new copy. 在某些时候,您可能正在复制矢量,但是您将原始矢量的迭代器与新副本一起使用。

In this case the culprit is caused by returning a copy of a vector or by passing a copy of a vector to a function along with iterators that belong to the original. 在这种情况下,罪魁祸首是由返回向量的副本将向量的副本与属于原始对象的迭代器一起传递给函数引起的。 You will need to review your code and look at how the vector and iteratos are being passed around. 您将需要检查您的代码,并查看如何传递vector和iterato。 Any where that a copy is made is a potential source of the problem. 复制的任何地方都可能是问题的根源。

Also, as Roy pointed out this can also happen if you hold an old iterator after performing a modification of certain containers. 另外,正如Roy所指出的,如果在修改某些容器后持有旧的迭代器,也会发生这种情况。 In the case of vector any time you add or remove an element the internal storage may need to be resized causing all iterators to become invalid. 在使用vector的情况下,无论何时添加或删除元素,都可能需要调整内部存储器的大小,从而导致所有迭代器均无效。 Any attempt to use those iterators causes undefined behavior and anything can happen. 任何使用这些迭代器的尝试都会导致未定义的行为,并且任何事情都可能发生。

normally it's because you have some actions changed the vector after you saved a iterator, eg insert/erase, the previously saved iterator is no longer validate. 通常是因为在保存迭代器后您有一些操作更改了向量,例如插入/擦除,因此先前保存的迭代器不再有效。

check your code to see if that is the case. 检查您的代码,看是否是这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM