92 Time Class Case Study--流程 1. Edit 2,. Preprocess宏、文件包含、条件编译 3. Compile编译缙误(语法等) 4. Link 5. Load 6. Execute Runtime error(运行的增说,Ftml致命错误 logic逻辑错误 0 2018, SEU. All rights reserved. 6
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 6 1. Edit 2. Preprocess 宏、文件包含、条件编译 3. Compile 编译错误(语法等) 4. Link 5. Load 6. Execute Runtime Error(运行时错误),Fatal致命错误,Logic逻辑错误 9.2 Time Class Case Study --- 流程
9.2 Time Class case stud Preprocessor Wrapper 现象:头文件被重复引用 est. cpp预处理 include结果 ∥/ah //test. cpp class af int a, class Af #include"a.h" #include"bh class af int a 1 int a class b double b:1 } int maino ∥/b.h int maino #include a h Aa class Bo Bb. Aa double b: return 0: Bb } return UR error C2011:A:class' type redefinition 0 2018, SEU. All rights reserved. 7
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 7 9.2 Time Class Case Study --- Preprocessor Wrapper 现象:头文件被重复引用 // a.h class A{ int a; }; // b.h #include "a.h" class B{ double b; }; // test.cpp #include "a.h" #include "b.h" int main() { A a; B b; return 0; } class A{ int a;} class A{ int a;} class B{ double b;} int main() { A a; B b; return 0; } Test.cpp预处理include结果 error C2011: 'A' : 'class' type redefinition
92 Time Class Case Study-预处理器指令 #define #define pl 3.1415 #define flag 预处理器定义 o#ifdef /#ifndef o #else o #endif 0 2018, SEU. All rights reserved. 8
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 8 9.2 Time Class Case Study --- 预处理器指令 #define #define PI 3.1415 #define FLAG 预处理器定义 #ifdef / #ifndef #else #endif
9.2 Time Class case stud 条件编译 般情况下,源程序中所有的行都参加编译;但 是有时希望其中一部分内容只在满足一定条件才 进行编译,也就是对一部分内容指定编译的条件, 这就是“条件编译”。 #ifndef flag #ifdef flag cout<<“代码1″<< endl cout<<“代码1"<<endl #endif #else cout<<“代码2”<<endl #ifdef flag #endif cout<<“代码2”<endl #endif 0 2018, SEU. All rights reserved. 9
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 9 9.2 Time Class Case Study --- 条件编译 一般情况下,源程序中所有的行都参加编译;但 是有时希望其中一部分内容只在满足一定条件才 进行编译,也就是对一部分内容指定编译的条件, 这就是“条件编译” 。 #ifndef FLAG cout << “代码1” << endl; #endif #ifdef FLAG cout << “代码2” << endl; #endif #ifdef FLAG cout << “代码1” << endl; #else cout << “代码2” << endl; #endif
9.2 Time Class Case Study Preprocessor Wrapper Test. cpp预处理 include结果 ∥/ah //test. cpp #ifndef h #ifndef h #include ah #define h #define h #include"bh" class af int a; 1 class Af #endif int a. int maino } #ifndef h #endif A #define h class af int a 1 Ibh Bb #endif #includeh return 0: class bi double b] class bi double b } 0 2018, SEU. All rights reserved. 10
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 10 9.2 Time Class Case Study --- Preprocessor Wrapper // a.h #ifndef A_H #define A_H class A{ int a; }; #endif // b.h #include "a.h" class B{ double b; }; // test.cpp #include "a.h" #include "b.h" int main() { A a; B b; return 0; } #ifndef A_H #define A_H class A{ int a;} #endif #ifndef A_H #define A_H class A{ int a;} #endif class B{ double b;} Test.cpp预处理include结果