Hi everyone,

A couple of questions, please. 1) In my Airports class I am trying to get input data from a text file and put it inside the variables of an airport object that I'm going to create. 2) Then I'm going to try and store my Airport objects inside a map container with a key being the Airport code. This is what it looks like so far.

This is my header file:

#ifndef AIRPORTS_
#define AIRPORTS_
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <map>
#include"Flights.h"
using namespace std;

class Airports{


 std::istream& operator (std::istream&, Airport::Airport&);
 
   };
 
 #endif /*AIRPORTS_*/

this is the CPP file:

#include <iostream>
#include <string>
#include "Airports.h"
      
     
      
    istream& Airports:: operator (istream& ins, Airport::Airports:: Airport& airport ){
  
  // Notice how this operator does not need to be 'friend' to Airport
  string s;
  int h, m;

  // Get Flight Code
  ins >> s;
  airport.setCode( s );

  // Get scheduled arrival time
  ins >> h;
  if (ins.get() != ':')
    {
    ins.setstate( ios::badbit );
    return ins;
    }
  ins >> m;
  airport.setMinimumConnect( (double)(h % 24) + (m / 60.0) );

  // Get the description for humans
  ins >> ws;
  getline( ins, s );
  airport.setDescription( s );

  return ins;
  } 
   
      
   // This is where the list of airline flight data is stored using airport code (to access airport information) 
map<Airport::Airport> airport_flights;
map<Airport,string> mymap;
  map<Airport,string>::iterator it;

// How many data do we need to read?
int num_flights;
cin >> num_flights;
cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );

// Read exactly that many
copy_n(
  std::istream_iterator <Airport::Airport> ( cin ),
  num_flights,
  std::back_insert_iterator <Airport::Airport> ( airport_flights )
  );

the above code doesn't compile.
these are the classes that the Airports class is using. the code below does compile and works:

#include "Flights.h"


		  //_____________Start Implementation of Flight class________________\\


		  /*Start House-keeping functions  for Flight Class
		   * Default constructor
		   *
		   */
		   Flight:: Flight (){
		   //debug statement
		   cout<< "we are in the construtor with Default arguments\n";

		   setOrigin("");
		   setDestination("");
		   setDepartureTime(0);
		   setDuration(0) ;


		 }

		  /* Constructor for flight object
		   *
		   *
		   */
		   Flight::Flight(const string &orig ,const string &destin ,const string &dep, const string &dur ){
			//debug statement
		   cout<< "we are in the construtor with arguments\n";
		 
		   setOrigin(orig);
		   setDestination(destin);
		   setDepartureTime(dep);
		   setDuration(dur);



		 }

		 /* copy constructor for flight object
		   *
		   *
		   *

		   FlightsTL:: Flight:: Flight(const Flight& rhs){



		   } */

		   /**destructor for flight object
		   *
		   *
		   */
		  Flight::  ~Flight(){

		 }
		   //end House-keeping functions \\


	   //_____________Start other functions____________________________\\

		   /* Equals operation
		   *
		   */
		  Flight&  Flight::operator=(const Flight& rhs){

			return *this;
		 }

		   /* get origin
		   *
		   */
		 string  Flight::getOrigin() const{

			  return org;
		   }

		   /* get destination
		   *
		   */
		 string  Flight::getDestination()const{

			  return dest;

		   }

		   /* get departure time
		   *
		   */
		   string  Flight:: getDepartureTime()const{

				return depTime;

		   }

		   /* get duration
		   *
		   */
		   string  Flight:: getDuration ()const{

			   return dura;

		   }

		   /* set origin
		   *
		   */
		   void  Flight:: setOrigin(string t ){

				  org = t;
		   }

		   /* set destination
		   *
		   */
		   void  Flight:: setDestination(string s){

				  dest =s;

		   }

		   /* set departure time
		   *
		   */
		   void  Flight:: setDepartureTime(string c){

			  depTime = c;
			}

		   /* get duration
		   *
		   */
		   void  Flight:: setDuration(string d){

				dura = d;
		   }

		   /* set all
		   *
		   */

		   void  Flight:: setAll(string a,string b,string c,string d){

				   setOrigin(a);
				   setDestination(b);
				   setDepartureTime(c);
				   setDuration(d) ;
		   }

		   /** ToString to display flight information
		   *
		   */
		   string  Flight:: toString()const{
				  string s;
				  //double depT = getDepartureTime();
				 // double durA =  getDuration();
				   // convert double to string
				 // char deptStr[50];
				 // sprintf(deptStr,"%g",depT);
				  // convert to string
				 // char durStr[50];
				//  sprintf(durStr,"%g",durA);
				  // print all Flight information
				  s=getOrigin() +"\t"+ getDestination() + "\t"+  getDepartureTime() + "\t"+ getDuration() +"\n" ;
					return s;
			   }


	//__________________end of Flight implementation_____________\\






	//__________________Start of of FlightsTL implementation_____________\\
        //Start House-keeping functions
		  /*Default constructor
		  *
		  */

	   Airport:: Airport (){
		setCode("");
		setMinimumConnect(0);
		setDescription("");


		 }

		  /* Constructor for Airport object
		   *
		   *
		   */
		 Airport:: Airport(const string& code, const double& minCon, const string& description){
			 setCode(code);
			  setMinimumConnect(minCon);
			  setDescription(description);

		  }

		  /* copy constructor for Airport object
		   *
		   *
		   */

		  // Airport(const Airport& rhs);

		   /**destructor for Airport object
		   *
		   *
		   */
		  Airport:: ~Airport(){



		   }

		   //end House-keeping functions\\

 //_________________Start of other Airport functions___________________________\\

		   /* Equals operation
		   *
		   */
		Airport&  Airport::  operator=(const Airport& rhs){

					  return *this;

		   }

		   /* get minimum connection
		   *
		   */
		  double Airport:: getMinimumConnectionTime()const{

							return minConnect;

		   }

		   /* get Code
		   *
		   */
		   string Airport:: getCode()const{
				  return code;


		   }

		   /* get description
		   *
		   */
		   string Airport:: getDescription()const{


				  return description;


		   }
		   
		   /*get number of Flights 
		   *
		   */
		   int Airport:: getFlightsNum(){
               
               return FlightsNum;
               
               }


		   /* set minimum connection time
		   *
		   */
		   void Airport:: setMinimumConnect (double a){

					 minConnect =a;

		   }

		   /* set code
		   *
		   */
		   void Airport:: setCode (string b){
						code = b;


		   }

		   /* set description
		   *
		   */
		   void Airport:: setDescription (string c){

					description = c;

		   }



		   /* set all
		   *
		   */

		   void Airport:: setAll (string a,double b,string c){

				   setMinimumConnect(b) ;
				   setCode(a);
				   setDescription(c);


		   }


		 void Airport::addFlight(const Flight& Flight){

			 Flights.push_back(Flight);
			 FlightsNum++;

		 }


		 /*add Flight
		  *
		  */
		 void Airport:: addNewFlight(const string& origin , const string& destination,const string& depTime, const string& duration){

			  Flights.push_back(Flight(origin,destination,depTime,duration));
			  FlightsNum++;

		   }

			/** ToString to display all flights and thier information
		   *
		   */
		 string  Airport:: toString(){
					  vector<Flight>::iterator find;
					  string str;

					  if (getFlightsNum() == 0) {
							return "Nothing to show no Flights" ;
					 }

				for(find = Flights.begin(); find != Flights.end(); find++){
					  str+=(*find).toString();

				   }
			

				  string s;
				  double mCon = getMinimumConnectionTime();

				   // convert double to string
				  char mConStr[50];
				  sprintf(mConStr,"%g",mCon);

				  // print all Airport information
				  s=getCode() +"\t"+ mConStr + "\t"+ getDescription() + "\n" ;
				 return str + s;

		   }



	  //__________________end of FlightsTL Implementation______________\\




	  //I'm thinking of adding remove(), and clear() and add via text

Thanks for your help, guys.

Jay F

Please, is there anyone able come up with a solution?

Hello is anyone there? i really need help please.

What is the error what you get and what compiler are you using.

here are the errors::'(

4 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp In file included from Airports.cpp

15 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.h expected identifier before '(' token

15 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.h abstract declarator `std::istream&' used as declaration

15 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.h expected `;' before '(' token

Thanks for taking a look

42 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected constructor, destructor, or type conversion before '>>' token

42 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected `,' or `;' before '>>' token

43 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected constructor, destructor, or type conversion before '.' token

43 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected `,' or `;' before '.' token

46 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected constructor, destructor, or type conversion before '(' token

46 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected `,' or `;' before '(' token

C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Makefile.win [Build Error] [Airports.o] Error 1

What is the error what you get and what compiler are you using.

compiler is Dev c++ MinGW compiler

Please flag each file with its exact file name. Two, some of these errors don't match the line numbers. Please make sure the line numbers you've listed correspond to the error message or point us to the REAL line number from where you've posted. Example:

42 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected constructor, destructor, or type conversion before '>>' token

Is this error in line 42? Here is line 42 of Airports.cpp that you posted.

// How many data do we need to read?

That can't be the error. It's a comment. Please provide accurate line numbers for the errors. Thanks.

I am really very sorry, the editor i am using isnt providing line numbers but here is the best i can do:

error:
4 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp In file included from Airports.cpp

#include "Airports.h"

15 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.h expected identifier before '(' token

std::istream& operator (std::istream&, Airport::Airport&);

15 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.h abstract declarator `std::istream&' used as declaration

std::istream& operator (std::istream&, Airport::Airport&);

15 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.h expected `;' before '(' token

std::istream& operator (std::istream&, Airport::Airport&);

42 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected constructor, destructor, or type conversion before '>>' token

cin >> num_flights;

42 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected `,' or `;' before '>>' token

cin >> num_flights;

43 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected constructor, destructor, or type conversion before '.' token

cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );

15 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.h abstract declarator `std::istream&' used as declaration

std::istream& operator (std::istream&, Airport::Airport&);

8 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp no `std::istream& Airports::$_36(std::istream&, Airport&)' member function declared in class `Airports'

istream& Airports:: operator (istream& ins, Airport::Airports:: Airport& airport ){

46 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected `,' or `;' before '(' token

copy_n(

43 C:\Documents and Settings\mugjy001\Desktop\Dev c++ ass3\Airports.cpp expected `,' or `;' before '.' token

cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );

that smiley not meant to be there

FYI, there is the 'Disable smilies in text' option below the 'Submit Reply' button ... ;)

Well, for this error:

std::istream& operator (std::istream&, Airport::Airport&);

what is the operator? For example, if you are defining the << operator, it would be something like this, I think (it's been a while since I've defined operator functions):

std::istream& operator << (std::istream&, Airport::Airport&);

Anyway, you have to define somewhere what operator you are referring to.

In this code:

#include <iostream>
#include <string>
#include "Airports.h"
      
     
      
    istream& Airports:: operator (istream& ins, Airport::Airports:: Airport& airport ){
  
  // Notice how this operator does not need to be 'friend' to Airport
  string s;
  int h, m;

  // Get Flight Code
  ins >> s;
  airport.setCode( s );

  // Get scheduled arrival time
  ins >> h;
  if (ins.get() != ':')
    {
    ins.setstate( ios::badbit );
    return ins;
    }
  ins >> m;
  airport.setMinimumConnect( (double)(h % 24) + (m / 60.0) );

  // Get the description for humans
  ins >> ws;
  getline( ins, s );
  airport.setDescription( s );

  return ins;
  } 
   
      
   // This is where the list of airline flight data is stored using airport code (to access airport information) 
map<Airport::Airport> airport_flights;
map<Airport,string> mymap;
  map<Airport,string>::iterator it;

// How many data do we need to read?
int num_flights;
cin >> num_flights;
cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );

// Read exactly that many
copy_n(
  std::istream_iterator <Airport::Airport> ( cin ),
  num_flights,
  std::back_insert_iterator <Airport::Airport> ( airport_flights )
  );

it looks to me like your function ends in line 33. Everything after that is just floating around and isn't inside any function. It seems like you should have gotten even more errors than you did, unless I'm missing something.

This article has been dead for over six months. Start a new discussion instead.