Multiple file woes

This is a discussion on Multiple file woes within the C++ Programming forums, part of the General Programming Boards category; I am trying to use .h and .cpp file in a program that I working on. I can not seem ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Multiple file woes

    I am trying to use .h and .cpp file in a program that I working on. I can not seem to get it to work.

    Is this the correct format?

    This is my .h file
    Code:
    //big.h
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    class foo
    { public:
          void BlaBla);
    
       private:
    
    };

    This is the .cpp file
    Code:
    // big.cpp 
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include "big.h"
    
    void BlaBla()
    {  ...
        ...
    }

    This is my main file
    Code:
    #include <iostram>
    #include <iomanip>
    #include <string>
    #include "big.h"
    
    using namespace.std;
    
    int main()
    {    ...
          ...
       
         function from Big.cpp file
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    looks mostly correct, minus a few typos. Headers usually start with #ifndef, then a #define, and end with #endif to prevent things from being included more than once. So for your example big.h would be like
    Code:
    #ifndef BIG_H
    #define BIG_H
    //stuff in here
    #endif

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    void BlaBla()
    {  ...
        ...
    }
    If this is suposed to be part of the foo class
    Code:
    void foo::BlaBla()
    {  ...
        ...
    }

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Here we go

    Ok I think I understand what is going on but I still get

    " gcc: Compilation of header file requested
    gcc: file path prefix `C:\DEV-C_~1\Bin\' never used "

    errors when I try to compile.

    Here is the actual code. When the Employee class and its functions were in the body of the main program, the program would compile and run. Now that I moved them to a .h and .cpp file I get the fore mentioned errors.

    Eventually I would like to remove all of the classes and their functions to separate files.

    Any suggestions would be appreciated.

    .h file
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    #ifndef Employee.h
    #define Employee.h
    
    class Employee
    { public:
         bool operator==(int find);
         virtual void getData() = 0;
         virtual void dsplyData() = 0;
      protected:
         string fName;               // Employee's first name
         string lName;               // Employee's last name
         int    age;                 // Employee's age
         int    empId;               // Employee's employee ID number
         float  salary;              // Employee's anual salary
         string school;              // School graduated from
    
    };
    
    #endif
    .cpp file
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include "Employee.h"
    
    using namespace std;
    
    void Employee::getData()         // Get employee information
    { cout<< " First Name:   " << endl
          << "->: ";
      cin>> fName;
      cout<< " Last Name:    " << endl
          << "->: ";
      cin>> lName;
      cout<< " Age:          " << endl
          << "->: ";
      cin>> age;
      cout<< " Employee ID:  " << endl
          << "->: ";
      cin>> empId;
      cout<< " Anual Salary: " << endl
          << "->: ";
      cin>> salary;  }
    
    void Employee::dsplyData()       // Display employee information
    { cout<< " Name: " << fName << " " << lName << endl;
      cout<< " Age: " << age << endl;
      cout<< " Employee ID: " << empId << endl;
      cout<< " Anual Salary: " << salary << endl;}
    
    bool Employee::operator==(int find)
    { if(empId == find)
        return true;
      return false;
    }
    main program
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include "Employee.h"
    
    using namespace std;
    
    
    /***** GLOBAL VARS & CONSTANTS */
    
    const int arraySize = 50;        // The size of the employee data base
    
    
    /***** CLASS DEFINITOINS *******/
    
    
    class Engineer : public Employee
    { public:
         Engineer(){};
         void getData();
         void dsplyData();
         ~Engineer(){};
    
      private:
         string discipline;          // Engineering disciplin.. Electrical/Chemical/Software/etc.
         string certs;               // Certifications.. PE/MSCD/MSCE/etc
    };
    
    class Manager : public Employee
    { public:
         Manager(){}
         void getData();
         void dsplyData();
         ~Manager(){};
    
      private:
         string deptManages;         // Department being managed
         int    numEmployees;        // Number of direct reports
    };
    
    class Scientist : public Employee
    { public:
         Scientist(){};
         void getData();
         void dsplyData();
         ~Scientist(){};
    
      private:
         string field;               // Field of study.. Chem/Biology/etc..
         int    numNobel;            // Number of Nobel Prizes won
         int    numPatents;          // Number of patents held
    };
    
    class Laborer : public Employee
    { public:
         Laborer(){};
         void getData();
         void dsplyData();
         ~Laborer(){};
    
      private:
         string specialty;           // Job specialty.. Electrical/sheet metal/back hoe/etc
         string unionMembership;     // The name of the union that the work belongs to
    };
    
    
    /***** PROGRAM BODY ************/
    
    int main()
    {
      Employee *database[arraySize];
      int index = 0;                 // Starting field for the employee database
      int choice;                    // Used in conjunctionn with the menu
      char exit = 'n';               // Used to exit the program when set to  Y'
      int find;                      // Used in the Search Option
    
    do
    { system ("cls");
      cout<< "********** EMPLOYEE DATABASE **********" << endl << endl;
      cout<< "  1. Enter a new Engineer              " << endl;
      cout<< "  2. Enter a new Manager               " << endl;
      cout<< "  3. Enter a new Scientist             " << endl;
      cout<< "  4. Enter a new Laborer               " << endl;
      cout<< "  5. View all employees                " << endl;
      cout<< "  6. Search for a Employee             " << endl;
      cout<< "  7. Exit                              " << endl << endl;
      cout<< "***************************************" << endl;
      cout<< "->: ";
      cin>> choice;
    
      switch (choice)
       { case 1:                     // Enter a new Engineer
           system("cls");
           if(index < arraySize)
             { cout<< "************* NEW ENGINEER ************" << endl << endl;
               database[index] = new Engineer;
               database[index] -> getData();
               ++index;
             }
             else
               { cout<< "The database is full " << endl << endl;
                 system("PAUSE");
               }
           break;
    
         case 2:                     // Enter a new Manager
           system("cls");
           if(index < arraySize)
             { cout<< "************* NEW Manager *************" << endl << endl;
               database[index] = new Manager;
               database[index] -> getData();
               ++index;
             }
             else
               { cout<< "The database is full " << endl << endl;
                 system("PAUSE");
               }
           break;
    
         case 3:                     // Enter a new Scientist
           system("cls");
           if(index < arraySize)
             { cout<< "************* NEW Scientist ***********" << endl << endl;
               database[index] = new Scientist;
               database[index] -> getData();
               ++index;
             }
             else
               { cout<< "The database is full " << endl << endl;
                 system("PAUSE");
               }
           break;
    
         case 4:                     // Enter a new Laborer
           system("cls");
           if(index < arraySize)
             { cout<< "************* NEW LABORER *************" << endl << endl;
               database[index] = new Laborer;
               database[index] -> getData();
               ++index;
             }
             else
               { cout<< "The database is full " << endl << endl;
                 system("PAUSE");
               }
           break;
    
         case 5:                     // Display all employees
            system("cls");
            cout<< "************* Employee Data **************" << endl << endl;
            for(int i=0; i < index; ++i)
              { database[i] -> dsplyData();
              }
            if (index == 0)
              cout<< "The database is empty " << endl;
            system("pause");
           break;
    
         case 6:                   // Search for employee by empId
           system("cls");
           cout<< "Enter An Employee Number Search for: " << endl
               << "->: ";
           cin>> find;
           system("cls");
           for(int i=0; i<index; i++)
             { if(*database[i]==find)
                 { database[i]->dsplyData();
                   break;
                 }
             }
           cout<< "Finished looking for " << find << endl << endl;
           system("Pause");
           break;
    
         case 7:                     // Exit the program
           exit = 'y';
           break;
    
         default:                    // Default error message
           cout<< "Please enter a valid option (1,2,3)" << endl << endl;
           choice = '\0';
           system("PAUSE");
           break;
       }
    } while(exit == 'n');
    
              return 0;
    }
    
    /***** FUNCTION DEFINITIONS ****/
    
    
    void Engineer::getData()         // Get Engineer information
    { Employee::getData();
      cout<< " School Graduated From: " << endl
          << "->: ";
      cin>> school;
      cout<< " Engineering Disipline (Eletrical, Chemical, Software, etc): " << endl
          << "->: ";
      cin>>  discipline;
      cout<< " Certifications (PE, MSCD, MSCE, etc): " << endl
          << "->: ";
      cin>> certs;
    }
    
    void Engineer::dsplyData()       // Display engineer information
    { cout<< "***************** ENGINEER *****************" << endl;
      Employee::dsplyData();
      cout<< " School Graduated From: " << school << endl;
      cout<< " Engineering Disipline: " << discipline << endl;
      cout<< " Certificatoins: " << certs << endl << endl;
      cout<< "____________________________________________" << endl << endl;
    }
    
    void Manager::getData()          // Get manager information
    { Employee::getData();
      cout<< " School Graduated From: " << endl
          << "->: ";
      cin>> school;
      cout<< " Department Manages: " << endl
          << "->: ";
          cin>> deptManages;
      cout<< " Number of Employees: " << endl
          << "->: ";
      cin>> numEmployees;
    }
    
    void Manager::dsplyData()        // Display manager information
    { cout<< "***************** MANAGER ******************" << endl;
      Employee::dsplyData();
      cout<< " School Graduated From: " << school << endl;
      cout<< " Deparment Manages: " << deptManages << endl;
      cout<< " Number of Employees" << numEmployees << endl << endl;
      cout<< "____________________________________________" << endl << endl;
    }
    
    void Scientist::getData()        // Get scientist information
    { Employee::getData();
      cout<< " School Graduated From: " << endl
          << "->: ";
      cin>> school;
      cout<< " Field (Chem, Biology, etc): " << endl
          << "->: ";
      cin>> field;
      cout<< " Number of Nobel Prizes in Technology: " << endl
          << "->: ";
      cin>> numNobel;
      cout<< " Number of Patents: " << endl
          << "->: ";
      cin>> numPatents;
    }
    
    void Scientist::dsplyData()        // Display scientist information
    { cout<< "***************** SCIENTIST ****************" << endl;
      Employee::dsplyData();
      cout<< " School Graduated From: " << school << endl;
      cout<< " Number of Nobel Prizes: " << numNobel << endl;
      cout<< " Number of Patents: " << numPatents << endl << endl;
      cout<< "____________________________________________" << endl << endl;
    }
    
    void Laborer::getData()          // Get laborer information
    { Employee::getData();
      cout<< " Specialty (Eletrical, Sheet Metal, Back Hoe, etc): " << endl
          << "->: ";
      cin>> specialty;
      cout<< " Union (IBEW, AFL-CIO, Teamsters, etc): " << endl
          << "->: ";
      cin>> unionMembership;
    }
    
    void Laborer::dsplyData()        // Display laborer information
    { cout<< "***************** LABORER ******************" << endl;
      Employee::dsplyData();
      cout<< " Specialty: " << specialty << endl;
      cout<< " Union: " << unionMembership << endl << endl;
      cout<< "____________________________________________" << endl << endl;
    }
    Thanks,

    Rich

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    periods aren't valid in #ifndef and #define, that's why _ is used. Besides that, I added the std namespace to the needed variables in the header. Using VC++ 2005 express starting with an empty console project, the code compiled fine for me with the mentioned changes.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Syneris,

    Thank you for your help.

    Your note about the console project reminded me that I was actually in a windows application project. Once I copied everything over to a console project everything was OK.

    Thanks again,

    Rich

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21