C ++" deque iterator not dereferencable"

时间:2014-12-08 16:14:59

标签: c++ iterator deque

我正在尝试使用deque编写一个模拟机场操作的程序,并继续运行“deque迭代器而不是可解除错误的错误。在搜索错误后,它说这通常是由于尝试从空队列弹出而引起的,但是我在我的代码中检查了一下。我无法弄清楚问题是什么。有什么建议吗?

转轮:

#include "airport.h"

using namespace std;

void menu();

bool badInput = false;
airport myAirport;

int main()
{
int numRunways;
int landingFreq;
int takeoffFreq;
int clockTime;

cout << "Welcome to CS1337 International Airport." << endl << "How many runways would you like to open today?" << endl;
do
{
    badInput = false;

    cin >> numRunways; //get input

    //if block steps through if non numerical input was entered
    if (cin.fail())
    {
        badInput = true; //flag bad input (non numerical value entered)
        //clear input stream
        cin.clear();
        cin.ignore(1000, '\n');
        //reprompt user
        cout << "Please enter only integer values." << endl;
    }
} while (badInput); //loop if bad input is entered

cout << endl << "How often do airplanes land at the airport? (1 to 100)" << endl;
do
{
    badInput = false;

    cin >> landingFreq; //get input

    //if block steps through if non numerical input was entered
    if (cin.fail())
    {
        badInput = true; //flag bad input (non numerical value entered)
        //clear input stream
        cin.clear();
        cin.ignore(1000, '\n');
        //reprompt user
        cout << "Please enter only integer values." << endl;
    }

    if (landingFreq > 100 || landingFreq < 1)
    {
        badInput = true;
        cout << "Please enter an integer between 1 and 100." << endl;
    }
} while (badInput); //loop if bad input is entered

cout << endl << "How often do airplanes takeoff from the airport? (1 to 100)" << endl;
do
{
    badInput = false;

    cin >> takeoffFreq; //get input

    //if block steps through if non numerical input was entered
    if (cin.fail())
    {
        badInput = true; //flag bad input (non numerical value entered)
        //clear input stream
        cin.clear();
        cin.ignore(1000, '\n');
        //reprompt user
        cout << "Please enter only integer values." << endl;
    }

    if (takeoffFreq > 100 || takeoffFreq < 1)
    {
        badInput = true;
        cout << "Please enter an integer between 1 and 100." << endl ;
    }
} while (badInput); //loop if bad input is entered

cout << endl << "How many minutes should the program wait between updates? (1 to 10)" << endl;
do
{
    badInput = false;

    cin >> clockTime; //get input

    //if block steps through if non numerical input was entered
    if (cin.fail())
    {
        badInput = true; //flag bad input (non numerical value entered)
        //clear input stream
        cin.clear();
        cin.ignore(1000, '\n');
        //reprompt user
        cout << "Please enter only integer values." << endl;
    }

    if (clockTime > 100 || clockTime < 1)
    {
        badInput = true;
        cout << "Please enter an integer between 1 and 10." << endl;
    }
} while (badInput); //loop if bad input is entered

myAirport = airport(numRunways, landingFreq, takeoffFreq, clockTime);

menu();

return 0;
}

void menu()
{
int inputSelect;
bool badInput;

cout << "What would you like to do?" << endl;
cout << "1. Tick the clock." << endl;
cout << "2. Get runway status." << endl;
cout << "3. Get airport statistics." << endl;
cout << "4. Exit program." << endl;

do
{
    cin >> inputSelect;
    badInput = false;

    cin >> inputSelect; //get input

    //if block steps through if non numerical input was entered
    if (cin.fail())
    {
        badInput = true; //flag bad input (non numerical value entered)
        //clear input stream
        cin.clear();
        cin.ignore(1000, '\n');
        //reprompt user
        cout << "Please enter only integer values." << endl;
    }

    if (inputSelect > 4 || inputSelect < 1)
    {
        badInput = true;
        cout << "Please enter an integer between 1 and 10." << endl;
    }
} while (badInput); //loop if bad input is entered

switch (inputSelect)
{
case 1: 
    myAirport.tick();
    break;

case 2:
    for (int i = 0; i < myAirport.numRunways(); i++)
    {
        cout << myAirport.runwayStatus(i);
    }
    break;

//case 3:

case 4:
    exit(0);
    break;
}

menu();
}

airport.cpp:

    #include "airport.h"


airport::airport(int numRunways, int aLandingFreq, int aTakeoffFreq, int aClockTime)
{
    landingFreq = aLandingFreq;
    takeoffFreq = aTakeoffFreq;
    clockTime = aClockTime;

    for (int i = 0; i < numRunways; i++)
    {
        myRunways.push_back(runway());
    }
}

airport::airport()
{
    landingFreq = 0;
    takeoffFreq = 0;
    clockTime = 0;
    myRunways.push_back(runway());
}

int airport::numRunways()
{
    return myRunways.size();
}

string airport::runwayStatus(int runwayNumber)
{
    return
        "Runway number " + to_string(runwayNumber) + " currently has " +
        to_string(myRunways[runwayNumber].getTakeoffPlanes()) + " planes waiting to takeoff and " +
        to_string(myRunways[runwayNumber].getlandingPlanes()) + " planes waiting to land. The total wait time is " +
        to_string(myRunways[runwayNumber].getTotalWaitTime()) + " minutes. The waiting time to land is " +
        to_string(myRunways[runwayNumber].getLandingWaitTime()) + " minutes.";
}

void airport::tick()
{
    int currentClock = clockTime;

    int landingProb = rand() % 100;
    int takeoffProb = rand() % 100;

    if (landingProb < landingFreq)
        landingArrive();
    if (takeoffProb < takeoffFreq)
        takeoffArrive();

    for (int i = 0; i < myRunways.size(); i++)
    {
        do
        {
            if (myRunways[i].getlandingPlanes())
            {
                if (myRunways[i].getNextPlane("land").getTime() < currentClock)
                {
                    currentClock = currentClock - myRunways[i].getNextPlane("land").getTime();
                    myRunways[i].removePlane("land");
                    cout << "A plane has landed on runway " << i << "." << endl;
                }
                else
                    myRunways[i].getNextPlane("land").setTime(myRunways[i].getNextPlane("land").getTime() - currentClock);
            }

            else if (myRunways[i].getTakeoffPlanes())
            {
                if (myRunways[i].getNextPlane("takeoff").getTime() < currentClock)
                {
                    currentClock = currentClock - myRunways[i].getNextPlane("takeoff").getTime();
                    myRunways[i].removePlane("takeoff");
                    cout << "A plane has taken off from runway " << i << "." << endl;
                }
                else
                    myRunways[i].getNextPlane("land").setTime(myRunways[i].getNextPlane("land").getTime() - currentClock);
            }
        } while (currentClock > 0);
    }

    for (int i = 0; i < myRunways.size(); i++)
    {
        myRunways[i].addTimeInQueue(clockTime);
    }
}

void airport::landingArrive()
{
    int minTime = 0;
    int minID = 0;

    int time = 0;
    bool badInput;

    cout << "A new plane has arrived to land. How long does this plane take to land?" << endl;

    do
    {
        badInput = false;

        cin >> time; //get input

        //if block steps through if non numerical input was entered
        if (cin.fail())
        {
            badInput = true; //flag bad input (non numerical value entered)
            //clear input stream
            cin.clear();
            cin.ignore(1000, '\n');
            //reprompt user
            cout << "Please enter only integer values." << endl;
        }
    } while (badInput); //loop if bad input is entered

    for (int i = 0; i < myRunways.size(); i++)
    {
        if (myRunways[i].getLandingWaitTime() <= minTime)
            minID = i;
    }

    myRunways[minID].addPlane(airplane("land", time));
}

void airport::takeoffArrive()
{
    int minTime = 0;
    int minID = 0;

    int time = 0;
    bool badInput = false;

    cout << "A new plane has arrived to takeoff. How long does this plane take to takeoff?" << endl;

    do
    {
        cin >> time; //get input

        //if block steps through if non numerical input was entered
        if (cin.fail())
        {
            badInput = true; //flag bad input (non numerical value entered)
            //clear input stream
            cin.clear();
            cin.ignore(1000, '\n');
            //reprompt user
            cout << "Please enter only integer values." << endl;
        }
    } while (badInput); //loop if bad input is entered

    for (int i = 0; i < myRunways.size(); i++)
    {
        if (myRunways[i].getTotalWaitTime() <= minTime)
            minID = i;
    }

    myRunways[minID].addPlane(airplane("takeoff", time));
}



airport::~airport()
{
}

runway.cpp:

#include "runway.h"


runway::runway()
{
    totalWaitTime = 0;
    landingWaitTime = 0;
}

int runway::getLandingWaitTime()
{
    return landingWaitTime;
}

int runway::getTotalWaitTime()
{
    return totalWaitTime;
}

void runway::addPlane(airplane aPlane)
{
    string type = aPlane.getType();

    if (type == "land")
    {
        landings.push_back(aPlane);
        landingWaitTime = landingWaitTime + aPlane.getTime();
    }
    else 
        takeoffs.push_back(aPlane);

    totalWaitTime = totalWaitTime + aPlane.getTime();
}

void runway::removePlane(string type)
{
    airplane removePlane;

    if (type == "land")
    {
        removePlane = landings.front();
        landingWaitTime = landingWaitTime - removePlane.getTime();
        landings.pop_front();
    }
    else
    {
        removePlane = takeoffs.front();
        takeoffs.pop_front();
    }

    totalWaitTime = totalWaitTime - removePlane.getTime();
}

airplane runway::getNextPlane(string type)
{
    //if (!landings.empty())
    //{
        if (type == "land")
            return landings.front();
        else
            return takeoffs.front();
    //}

    //else return 
}

bool runway::getlandingPlanes()
{
    return landings.empty();
}

bool runway::getTakeoffPlanes()
{
    return takeoffs.empty();
}

void runway::addTimeInQueue(int aTime)
{
    for (int i = 0; i < landings.size(); i++)
    {
        landings[i].addTimeInQueue(aTime);
    }

    for (int i = 0; i < takeoffs.size(); i++)
    {
        takeoffs[i].addTimeInQueue(aTime);
    }
}



runway::~runway()
{
}

airplane.cpp:

#include "airplane.h"


airplane::airplane(string aType, int aTime)
{
    type = aType;
    time = aTime;
}

airplane::airplane()
{
    type = "takeoff";
    time = 0;
}

void airplane::setType(string aType)
{
    type = aType;
}

void airplane::setTime(int aTime)
{
    time = aTime;
}

void airplane::addTimeInQueue(int aTimeInQueue)
{
    timeInQueue = timeInQueue + aTimeInQueue;
}

string airplane::getType()
{
    return type;
}

int airplane::getTime()
{
    return time;
}

int airplane::getTimeInQueue()
{
    return timeInQueue;
}

airplane::~airplane()
{
}

1 个答案:

答案 0 :(得分:2)

bool runway::getlandingPlanes()
{
    return landings.empty();
}

如果没有登陆平面,则返回true。您将结果解释为表示 某些着陆层(正如函数名称所暗示的那样)。你应该改变返回值的意义:

return !landings.empty();
       ^

同样适用于getTakeoffPlanes