I'm still working on the same Flight Database as before. It's a program using 3 classes each with their own header and implementation files. The 3 classes are Date, Flight, and Trip.
The Date Class is only used within the Flight and Trip Classes.
So those two classes each have a #include "Date.h" directive at the beginning. Then in my driver program I have #include "Trip.h" and #include "Flight.h". When I go to run the makefile, I get an error it the driver program that says:
weise: make travel
g++ -c travel.cpp
In file included from Trip.h:5,
from travel.cpp:6:
Date.h:9: error: redefinition of `class Date'
Date.h:9: error: previous definition of `class Date'
make: *** [travel.o] Error 1
I can't take one of the #include "Date.h" directives out of the headers because then the header will fail. How can I fix this? Below are the Flight and Trip Headers, as well as the beginning of my driver program. Any help at all would be greatly appreciated.
Trip.h
#include <iostream>
#include "Date.h"
using namespace std;
class Trip {
public:
// Initializers
Trip();
Trip(string _home, string _destin, Date _depart, Date _return);
// Accessors
string getHome() const;
string getDestin() const;
Date getDepart() const;
Date getReturn() const;
private:
string home, destin;
Date depart, Return;
} ;
Flight.h
#include <iostream>
#include "Date.h"
using namespace std;
class Flight {
public:
// Initializers
Flight();
Flight(int fNum, string _origin, string _dest, Date _dep, Date _ret);
// Accessors
int getFNum() const;
string getOrigin() const;
string getDest() const;
Date getDep() const;
Date getRet() const;
// uses overloaded operator to output the stored Date.
void input(istream&);
private:
int flightNum;
string origin, dest;
Date dep, ret;
} ;
istream& operator>> (istream&, Flight&);
Driver Program
#include <iostream>
#include <vector>
#include <fstream>
#include "Flight.h"
#include "Trip.h"
using namespace std;
int main() {
int m, d, y;
string city;
Flight flight;
char fName[100];
vector<Flight> flights;
cout << "Enter name of flight data file: ";
cin >> fName;
ifstream readFrom;
readFrom.open(fName);
if (readFrom.fail()) {
cout << endl << "Input file \"" << fName << "\" opening failed." << endl << endl;
return 1;
}
while(readFrom >> flight)
flights.push_back(flight);
readFrom.close();