Include versus Link - When shoud you use include, when shouldn't - Understand class definition, include directive, template function and compiling 1. Oridinary classes and functions -------------------------------------- (1) Program you write // mydata.h class mydata { public: typedef int Item; mydata(Item init = Item()) { data = init;} void print(); private: Item data; }; //mydata.cxx #include #include "mydata.h" void mydata::print() { cout << data < demo'.cxx class mydata { public: typedef int Item; mydata(Item init = Item()) { data = init;} void print(); private: Item data; }; int main() { mydata d(1); d.print(); return EXIT_SUCCESS; } step 2. turn source code to object code -------------------------------------------------- demo'.cxx -> demo.obj B. compile mydata.cxx step 1: iostream+mydata.h+mydata.cxx -> mydata'.cxx class mydata { public: typedef int Item; mydata(Item init = 0); void print(); private: Item data; }; void mydata::print() { cout << data < mydata.obj C. use link to add two object files and generate an executable file ----------------------------------------------------------------- link demo.obj + mydata.obj -> demo.exe In VC++ open demo.cxx compile it -> a project demo.dsp and demo.obj add mydata.cxx to the projectr demo.dsp compile mydata.cxx -> mydata.obj or directly bulid demo.exe : compile demo.cxx if it is updated ->demo.obj compile mydata.cxx if it is updated -> mydata.obj link demo.obj + mydata.obj -> demo.exe (3). Run demo.exe XXXXXXXXXXXXXXXXXXXXXXXXXX Notes: XXXXXXXXXXXXXXXXXXXxxxxXXX - Don't compile a header file such as mydata.h (cannot be compiled) - Don't include mydata.cxx (or .cpp) file into demo.cxx (or .cpp) file - even though you might be allowed to do so by the compiler otherwise the compiler will compile mydata.cxx with demo.cxx evrey time even if you do not make any change to mydata.cxx it is lot of work if mydata.cxx is huge - Never include mydata.cxx into demo.cxx AND add mydata.cxx in project demo.dsp - you will have link errors - redifintion 2. Template Class and Functions ======================================== (1) Program you write // mydata.h template class mydata { public: mydata(Item init = Item()) { data = init;} void print(); private: Item data; }; #include "mydata.template" //mydata.template #include // #include "mydata.h" // remove this - since mydata.template is // included in mydata.h template void mydata::print() { cout << data < #include "mydata.h" int main() { mydata i(2); mydate s("Hello"); i.print(); s.print(); return EXIT_SUCCESS; } (2) Compiling A compile demo.cxx step 1: use include directive to put two files into one file by the compiler ----------------------------------------- demo.cxx+mydata.h +mydata.template-> demo'.cxx ................................ the code from comes here ................................ template class mydata { public: mydata(Item init = Item()) { data = init;} void print(); private: Item data; }; template void mydata::print() { cout << data < i(2); mydate s("Hello"); i.print(); s.print(); return EXIT_SUCCESS; } step 2. turn source code to object code: Instantiation -------------------------------------------------- demo'.cxx -> demo.obj object code could be the object of something like ++++++++++++++++++++++++++++++++++++++++++++++++++++ ................................ the code from comes here ................................ class mydata_i { public: mydata(int init = int()) { data = init;} void print(); private: int data; }; void mydata_i::print() { cout << data < demo.exe In VC++ open demo.cxx compile it -> a project demo.dsp and demo.obj (Don't add mydata.template to the project demo.dsp!) (Don't compile mydata.template) OR directly bulid demo.exe : compile demo.cxx if it is updated ->demo.obj link demo.obj -> demo.exe (3). Run demo.exe XXXXXXXXXXXXXXXXXXXXXXXXXX Notes: XXXXXXXXXXXXXXXXXXXxxxxXXX - Don't compile a header file such as mydata.h (cannot be compiled) - Never add mydata.template in project demo.dsp - you cannot compile mydata.template file (with unspecified type Item) - Inlcuding mydata.template into mydata.h automatically includes it into demo.cxx file, where the instantiations are done! ******* That's why the template implementation file is included in the header file!