c++ - Precompiled header error -
i have simple program 3 files(excluding precompiled header). have included precompiled header in .cpp files yet giving these 2 errors don't seem understand :
1>d:\practice\operatoroverloading\oopinheritance\oopinheritance\commisionemp.h(4): warning c4627: '#include ': skipped when looking precompiled header use 1> add directive 'stdafx.h' or rebuild precompiled header 1>d:\practice\operatoroverloading\oopinheritance\oopinheritance\commisionemp.h(35): fatal error c1010: unexpected end of file while looking precompiled header. did forget add '#include "stdafx.h"' source? ========== rebuild all: 0 succeeded, 1 failed, 0 skipped ==========
here codes:
oopinheritance.cpp // oopinheritance.cpp : defines entry point 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 functions:\n" <<"\n first name is" <<employee.getfirstname() <<"\n last name "<<employee.getlastname() <<"\nsocial security number is" <<employee.getsocialsecuritynumber() <<"\ngross sales " <<employee.getgrosssales() <<"\ncommission rate " <<employee.getcommissionrate()<<endl; employee.setgrosssales(8000); employee.setcommissionrate(.1); cout<<"\n updated employee infromation output 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 >=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 > 0.0 , < 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; }
precompiled headers in vs work (i invented generic names use, don't try googling them):
take header file; default, project wizard call stdafx.h (a stupid historic name). put stuff include everywhere in there. header pch source header.
configure project bysetting "use precompiled header" flag "use" , "precompiled header" flag pch source header.
take source file empty except including pch source header; called same header .cpp ending, wizard calls stdafx.cpp. pch reference file.
override "use precompiled header" flag "create" pch reference file.
the compiler compile reference file, , save internal state file; result .pch file. compiler can later restore internal state saved state , continue there.
in other files, compiler scan #include directive pch source header, emitting warnings if finds other code. when finds directive, restores state .pch , continues normal compilation there. because state restore operation overwrites existing internal state, nothing may come before include directive.
if include directive missing, emit fatal error. and, judging error, same happens if include directive exists, no .pch file can found, because don't have stdafx.cpp correctly configured.
of course, system is stupid, because highly specific way ms compiler works, , makes porting other compilers annoying. (you have useless header in every source file pulls in far more want.) means weird errors whenever create new source file , forget include stdafx.h. here's better way of doing it: remove #include "stdafx.h" directives. in project options, under c/c++ -> advanced, set stdafx.h forced include file. there's implicit #include directive @ start of every source file, can't forget it, , doesn't interfere other compilers.
and while you're @ it, rename pch file. use projectname_pch.h/cpp.
Comments
Post a Comment