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

Recommended Answers

All 5 Replies

Where is your "Date.h".

I didn't think it was necessary with the explanation I gave of what what I was attempting, but here it is:

date.h

#include <iostream>

using namespace std;

class Date {
   public:
      // initializers
      Date();
      Date(int day, int month, int year);

      // Accessors
      int day() const;
      int month() const;
      int year() const;
  
      // Date::valid returns true if the host object is a valid date.
      bool valid() const;

      // Comparison functions, to be used by overloaded operators
      bool equal(const Date&) const;
      bool earlierThan(const Date&) const;
  
      //IO functions uses overloaded operators to process data.
      void input(istream&);
      void output(ostream&) const;
   private:
      static bool checkDate(int d, int m, int y);
      int _day, _month, _year;
};

// IO operators
istream& operator>>(istream&, Date&);
ostream& operator<<(ostream&, const Date&);

// Comparison operators
bool operator==(const Date&, const Date&);
bool operator!=(const Date&, const Date&);
bool operator<(const Date&, const Date&);
bool operator<=(const Date&, const Date&);
bool operator>(const Date&, const Date&);
bool operator>=(const Date&, const Date&);

You havent define your class date

Class date d;

I am just guessing . Please check it.

I don't understand what you mean. Where am I supposed to define it?

I figured out what to do.

I had to add #ifndef.

so my Date.h ended up looking like this.

#ifndef DATE_H
#define DATE_H

#include <iostream>

using namespace std;

class Date {
   public:
      // initializers
      Date();
      Date(int day, int month, int year);

      // Accessors
      int day() const;
      int month() const;
      int year() const;
  
      // Date::valid returns true if the host object is a valid date.
      bool valid() const;

      // Comparison functions, to be used by overloaded operators
      bool equal(const Date&) const;
      bool earlierThan(const Date&) const;
  
      //IO functions uses overloaded operators to process data.
      void input(istream&);
      void output(ostream&) const;
   private:
      static bool checkDate(int d, int m, int y);
      int _day, _month, _year;
};

// IO operators
istream& operator>>(istream&, Date&);
ostream& operator<<(ostream&, const Date&);

// Comparison operators
bool operator==(const Date&, const Date&);
bool operator!=(const Date&, const Date&);
bool operator<(const Date&, const Date&);
bool operator<=(const Date&, const Date&);
bool operator>(const Date&, const Date&);
bool operator>=(const Date&, const Date&);

#endif
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.