-1

I have two classes. Portal and Airline. Both are virtual and abstract respectively. The code is really roughly made. Sorry for that. I have created derived classes of Airline and Portal respectively as AIR and POR.

Basically my main creates a portal pointer of derived class POR and then it is passed in the airlines derived class AIR. Airline has a constructor that takes portal pointer as an argument but i am confused as to how to make a constructor for AIR.

I get an error that I cannot understand. The class files are given to us and cannot be changed as per the professor. If there is any issue with the class file, please let me know.

Anyway here is the code:

class Portal {
public:
Portal() {}
virtual ~Portal() {}

enum SortField {Airline, Time, Duration, Price};
enum SortOrder {Ascending, Descending};
enum BuyOption {Cheapest, Fastest, Earliest, Latest};

// initialize route information by reading in input file with government data/guidelines
virtual void initRoutes(string routeInfo) {}

// return constraints/guidelines for a given origin+destination combination
virtual void routeInfo(string origin, string destination, float& dist, float& duration, float& minPrice, float& maxPrice, float& timeVariation) {}

// display (to cout) the list of flights with available seats between origin and destination
// List is to be shown in sorted order, based on sortField and sortOrder
// sortField is one of the values of enum SortField
// sortOrder is one of values of enum SortOrder
virtual void showFlights(string origin, string destination, SortField sortField = Airline, SortOrder sortOrder = Descending) {}

// purchase a ticket based on BuyOption criteria, optionally specifying a preferred airline
// This will be for the last selected origin+destination combination
virtual bool buyTicket(BuyOption cirteria, string airline = NULL) = 0;

};

class POR: public Portal{
protected:
vector<Route *> rt_list;
public:
void initRoutes(string routeInfo){

    int n=0;
    string line;

    float val;
    string str;

    ifstream rfile;
    rfile.open(routeInfo.c_str(),ios::in);
    if(rfile.is_open()){
        while(!rfile.eof()){
            getline(rfile,line);
            ++n;
        }
    }
    rfile.close();

    rfile.open(routeInfo.c_str(),ios::in);
    Route *ptr;

    while(!rfile.eof()){
        rfile >> str;
        ptr->setorigin(str);
        rfile >> str;
        ptr->setdestination(str);
        rfile >> val;
        ptr->setdistance(val);
        rfile >> val;
        ptr->setduration(val);
        rfile >> val;
        ptr->setminpkm(val);
        rfile >> val;
        ptr->setmaxpkm(val);
        rfile >> val;
        ptr->setdev(val);

        rt_list.push_back(ptr);
    }
    rfile.close();
}

void routeInfo(string origin, string destination, float& dist, float& duration,float& minPrice, float& maxPrice, float& timeVariation){
    int len = rt_list.size();
    string str1,str2;
    for(int i = 0;i < len;i++){
        str1 = rt_list[i]->getOrigin();
        str2 = rt_list[i]->getDestination();
        if((str1 == origin and str2 == destination) or (str1 == destination and str2 == origin)){
            cout<<rt_list[i]->getDistance();
            cout<<rt_list[i]->getDuration();
            cout<<rt_list[i]->getMinpkm() * rt_list[i]->getDistance();
            cout<<rt_list[i]->getMaxpkm() * rt_list[i]->getDistance();
            cout<<rt_list[i]->getDev();
        }
    }
}

void showFlights(string origin, string destination, SortField sortField = Airline,SortOrder sortOrder = Descending){

}

bool buyTicket(BuyOption cirteria, string airline = NULL){

}
};
class Airline {

private:
Portal *portal;

protected:
Portal *getPortal() {
    return portal;
}

public:

Airline(Portal *pl):portal(pl){

}

virtual ~Airline() {}

// reads in the input file for the airline and initializes its
// information about routes, capacity etc
virtual void init(string ifile) = 0;

virtual string getName() = 0; // the name of the airline. Should have the last 4 digits of the roll no.

// return the list of flights for this airline between origin and destination cities
// The list of flights is appended to the vector flights that is passed in.
// Input vector flights will not be a null reference
virtual void findFlights(string origin, string destination,
                         vector<Flight *>& flights) {}

// get the current price for a specified flight of this airline
virtual float getPrice(Flight *flight) = 0;

// buy a ticket from this airline for a particular flight
// Returns true if the ticket can be issues (i.e. there are seats available on the flight
virtual bool issueTicket(Flight *flight) = 0;

// number of tickets sold today
virtual int getNumSold() = 0;

// get total revenue and passenger km for the day
virtual void getSalesStats(float& revenue, float& passKm) {}

};

class AIR:public Airline{
protected:
POR *portal;
string name;
int seats,nflightsperday;
string o,d;
vector<string> org;
vector<string> dest;
vector<int> nflights;
vector<FLY *> flight_list;
public:
//CONSTRUCTOR ??????

/*
AIR(){
    ;
}
*/

AIR(POR *pl):portal(pl){
    name = "";
}

void init(string ifile){
    FLY *ptr;
    ifstream fin; 
    fin.open(ifile.c_str(), ios::in);
    fin >> seats;
    while(!fin.eof()){
        fin >> o >> d;
        org.push_back(o);
        dest.push_back(d);
        fin >> nflightsperday;
        nflights.push_back(nflightsperday);

        for(int i=0;i<nflightsperday;i++){
            ptr->setOrigin(o);
            ptr->setDestination(d);
            ptr->setName(ifile.c_str());
            flight_list.push_back(ptr);
        }

    }
    fin.close();
}

/*void readfile(ifstream& fin,string ifile){
    fin.open(ifile.c_str(), ios::in);
    fin >> seats;
    while(!fin.eof()){
        fin >> o >> d;
        org.push_back(o);
        dest.push_back(d);
        fin >> nflightsperday;
        nflights.push_back(nflightsperday);
    }
    fin.close();
}*/

string getName(){
    //cout<<typeid(obj).name();
    return "AIR";
}
void findFlights(string origin, string destination,  vector<Flight *>& flights){

}
float getPrice(Flight *flight){

}
bool issueTicket(Flight *flight){

}
int getNumSold(){
    return -1;
}
void getTicketStats(float& revenue, float& passKm){

}
};


int main(){
POR *x=new POR;
AIR a1(x);
return 0;
}

I'm getting such an error.

version4.cpp: In constructor ‘AIR::AIR(POR*)’:
version4.cpp:303:24: error: no matching function for call to ‘Airline::Airline()’
AIR(POR *pl):portal(pl){
                    ^
version4.cpp:303:24: note: candidates are:
version4.cpp:251:2: note: Airline::Airline(Portal*)
Airline(Portal *pl):portal(pl){
^
version4.cpp:251:2: note:   candidate expects 1 argument, 0 provided
version4.cpp:239:7: note: Airline::Airline(const Airline&)
class Airline {
      ^
version4.cpp:239:7: note:   candidate expects 1 argument, 0 provided
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Vishal
  • 1
  • Too long, too much code. Cut it down to a small program that demonstrates the issue. If you do that it might even help you find the problem. – mjs Oct 19 '16 at 20:51
  • When you define a constructor that accepts an argument, it deletes the default constructor – AndyG Oct 19 '16 at 21:04
  • You need to read about the use of `eof`[here](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – molbdnilo Oct 19 '16 at 21:12
  • Welcome to Stack Overflow! You can take the [tour] first and learn [ask] a good question. That makes it easier for us to help you. – Katie Oct 19 '16 at 21:15

1 Answers1

0

The construction of AIR also needs to construct the base class.
Airline doesn't have a default constructor, it only has one that accepts a Portal*.

Since Airline already has a portal member, you shouldn't define another one in AIR.

Instead you should pass the constructor's parameter on to the construction of Airline:

AIR::AIR(Portal* p) : Airline(p) 
{

}
molbdnilo
  • 55,783
  • 3
  • 31
  • 71