0
votes

I have a simple program with three files(excluding the precompiled header). I have included the precompiled header in the .cpp files Yet it is giving these two errors that I don't seem to understand :

1>d:\practice\operatoroverloading\oopinheritance\oopinheritance\commisionemp.h(4): warning C4627: '#include ': skipped when looking for precompiled header use 1> Add directive to 'StdAfx.h' or rebuild precompiled header 1>d:\practice\operatoroverloading\oopinheritance\oopinheritance\commisionemp.h(35): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source? ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Here are the codes:

OOPInheritance.cpp

        // OOPInheritance.cpp : Defines the entry point for the console application.
        //

        #include "stdafx.h"
        #include <iostream>
        #include <iomanip>
        #include "CommisionEmp.h"

        using namespace std;

        int _tmain(int argc, _TCHAR* argv[])
        {
            CommissionEmp employee("Dede","Kikadi","777-77-7777",5000,0.08);

            cout<<fixed<<setprecision(2);

            cout<<"Employee information obtained by get functions:\n"
                <<"\n First name is" <<employee.getFirstName()
                <<"\n Last name is "<<employee.getLastName()
                <<"\nSocial security number is"
                <<employee.getSocialSecurityNumber()
                <<"\nGross sales is " <<employee.getGrossSales()
                <<"\nCommission rate is " <<employee.getCommissionRate()<<endl;

            employee.setGrossSales(8000);
            employee.setCommissionRate(.1);

            cout<<"\n Updated employee infromation output by print function : \n"<<endl;
            employee.print();

            cout<<"\n Employee's earning : ZAR"<<employee.earnings()<<endl;
            return 0;
        }

CommisionEmp.h:

        #ifndef COMMISSION_H
        #define COMMISSION_H

        #include <string>
        using namespace std;

        class CommissionEmp{
        public:
            CommissionEmp(const string &, const string &, const string &, double=0.0, double =0.0);
            void setFirstName(const string &);
            string getFirstName() const;

            void setLastName(const string &);
            string getLastName() const;

            void setSocialSecurityNumber(const string &);
            string getSocialSecurityNumber() const;

            void setGrossSales(double);
            double getGrossSales() const;

            void setCommissionRate(double);
            double getCommissionRate() const;

            double earnings()const;
            void print() const;
        private:
            string firstName;
            string lastName;
            string socialSecurityNumber;
            double grossSales;
            double commissionRate;
        };
        #endif

CommissionEmp.cpp:

        #include "stdafx.h"
        #include <iostream>
        #include "CommisionEmp.h"

        using namespace std;

        CommissionEmp::CommissionEmp(const string &first, const string &last, const string &ssn, double sales, double rate) :
        firstName(first), lastName (last), socialSecurityNumber(ssn)
        {
            setGrossSales(sales);
            setCommissionRate(rate);
        }

        void CommissionEmp::setFirstName(const string &first)
        {
            firstName=first;
        }
        string CommissionEmp::getFirstName()const {return firstName;}

        void CommissionEmp::setLastName (const string &last)
        {
            lastName=last;
        }
        string CommissionEmp::getLastName()const {return lastName;}

        void CommissionEmp::setSocialSecurityNumber (const string &ssn)
        {
            socialSecurityNumber=ssn;
        }
        string CommissionEmp::getSocialSecurityNumber()const {return socialSecurityNumber;}

        void CommissionEmp::setGrossSales (double sales)
        {
            if(sales >= 0.0)
                grossSales=sales;
            else throw invalid_argument("Gross sales must be >=0.0");
        }
        double CommissionEmp::getGrossSales()const {return grossSales;}

        void CommissionEmp::setCommissionRate (double rate)
        {
            if(rate>0.0 && rate <1.0)
                commissionRate=rate;
            else throw invalid_argument("Commission rate must be > 0.0 and < 1.0");
        }
        double CommissionEmp::getCommissionRate()const {return commissionRate;}
        double CommissionEmp::earnings() const{
        return commissionRate*grossSales;
        }
        void CommissionEmp::print() const{
            cout<<"commission empoyee: " <<firstName <<' ' <<lastName
                <<"\n social security number : "<<socialSecurityNumber
                <<"\ngross sales: "<< grossSales
                <<"\ncommission rate: "<<commissionRate;
        }
1
Your error message talks about commisionemp.h, but none of your files are called that...Mats Petersson
@MatsPetersson Peterson, sorry I have put the names of the file now.panto111088

1 Answers

1
votes

Precompiled headers in VS work like this (I invented the generic names I use, so don't try googling for them):

Take a header file; by default, the project wizard will call it stdafx.h (a really stupid historic name). Put stuff you include almost everywhere in there. This header is the pch source header.

Configure the project bysetting the "Use precompiled header" flag to "Use" and the "Precompiled header" flag to your pch source header.

Take a source file that is empty except for including the pch source header; usually called the same as the header with a .cpp ending, so the wizard calls it stdafx.cpp. This is the pch reference file.

Override the "Use precompiled header" flag to "Create" for the pch reference file.

The compiler will now compile the reference file, and save its internal state to a file; the result is the .pch file. The compiler can later restore its internal state to the saved state and continue from there.

In all other files, the compiler will scan for an #include directive for the pch source header, emitting warnings if it finds any other code. When it finds the directive, it restores its state from the .pch and continues normal compilation from there. Because the state restore operation overwrites any existing internal state, nothing may come before the include directive.

If the include directive is missing, emit a fatal error. And, judging from your error, the same happens if the include directive exists, but no .pch file can be found, probably because you don't have a stdafx.cpp correctly configured.


Of course, the system as is is stupid, because it is highly specific to the way the MS compiler works, and makes porting to other compilers annoying. (You basically have a useless header in every source file that pulls in far more than you want.) It also means that you get weird errors whenever you create a new source file and forget to include stdafx.h. Here's a better way of doing it: remove all #include "stdafx.h" directives. In the project options, under C/C++ -> Advanced, set stdafx.h as a forced include file. Now there's an implicit #include directive at the start of every source file, so you can't forget it, and it doesn't interfere with other compilers.

And while you're at it, rename the pch file. I like to use projectname_pch.h/cpp.