简体   繁体   中英

Error: Vector subscript out of range c++

I hope somebody can help me with this.

I have tried to fix this program, but I don't know what else to so. I keep getting this error about a vector's subscript being out of range. However, I have added an object to each variable created in "main" using push_back, so I don't know what the problem is.

The problem is a kind of graph consisting of 9 cities. Each city is directly connected to one city. Using the function "shortpath", I add the parent of each city to the member previous1.

However, when I run the program, i keeps telling me that the size of the variable "neighbor1" of the cities is 0 even though I push_backed in the main.

#include<iostream>
#include<vector>
#include<queue>
#include<string>


using namespace std;

class City
{
public:

City();
City( string city );
string cityName;
int cityDistance;
vector<City> neighbor1;
bool visited1;
vector<City> previous1;
void addNeighbor( City c );
void shortPath( vector<City> cities );
void printPath( City final );

};

This is the implementation of the header file:

#include<iostream>
#include<vector>
#include<queue>
#include<string>


using namespace std;
#include"City.h"



City::City()
{
cityDistance = -1;
cityName = " ";
visited1 = false;

}
City::City( string city )
{
cityDistance = -1;
cityName = city;
visited1 = false;


}

void City::addNeighbor( City c )
{
neighbor1.push_back( 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();

}




}





void City::printPath( City final )
{
if( final.previous1.size() != 0 )
{
    cout << final.previous1[0].cityName << endl;
}
else
{
    cout << "No previous for final" << endl;
}

}  

and this is the main:

#include<iostream>
#include<vector>
#include<queue>
#include<string>


using namespace std;
#include"City.h"

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

HongKong.printPath( NewYork );







system("PAUSE");
return 0;
 }

You are copying Cities.

In vector1.push_back(HongKong); not the original City is added to vector1, but a copy.

After that, you do HongKong.neighbor1.push_back( NewDelhi); which only works on the original, not on the copy in vector .

So when you use vector1 in NewYork.shortPath( vector1 ); it doesn't contain the neighbours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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