oop(c第12章模板最终版ppt-第12章模板template内容摘要:
delete[ ] v。 } template class T void StackT:: push(T a) { *p++ = a}。 template class T T StackT:: pop() { return * p。 } template class T int StackT:: size() const { return pv。 } }。 《面向对象程序设计》 中国科学技术大学计算机系 马建辉 24 关于头文件 对类来说,在创建非内联函数定义时,我们通常把定义放在 .h文件中,而把实现放在 .cpp中。 (实际上是所谓的头文件原则: “ 在头文件中,不要放置分配存储空间的任何东西 ” ,为了防止在连接期间的多重定义错误。 ) 对模板来说,即使是在创建非内联函数定义时,模板的所有定义和实现都必须放入一个头文件中。 why。 因为模板很特殊,在 template… 之后的任何东西都意味着编译器在当时不为它分配存储空间(即不编译成目标代码),而是一直处于等待状态直到一个模板示例被告知。 《面向对象程序设计》 中国科学技术大学计算机系 马建辉 25 如果没有被实例化,模板不会被编译成目标代码: template class T class Stack{ T *v。 // 栈底 T *p。 // 栈顶 int sz。 // 栈的大小 public: Stack(int s。 ~Stack()。 void push(T a)。 T pop()。 int size()。 }。 文件: 《面向对象程序设计》 中国科学技术大学计算机系 马建辉 26 template class T StackT::Stack(int s){ v=p=new T[sz=s]。 } template class T StackT:: ~Stack() { delete[ ] v。 } template class T void StackT:: push(T a) { *p++ = a}。 template class T T StackT:: pop() { return * p。 } template class T int StackT:: size() const { return pv。 } }。 文件: 《面向对象程序设计》 中国科学技术大学计算机系 马建辉 27 include “” void main() { Stackint si(10)。 (1)。 // 连接错误 } 文件 : error LNK2020: unresolved external symbol public: int __thiscall Stackint::push(int) (?push@?$stack@H@@QAEHXZ) 发生连接错误: 《面向对象程序设计》 中国科学技术大学计算机系 马建辉 28 From MSDN: Linker Tools Error LNK2020 unresolved external symbol symbol Code will generate this error message if it references something (like a function, variable, or label) that the linker can’t find in all the libraries and object files it searches. In general, there are two reasons this error occurs: what the code asks for doesn’t exist (the symbol is spelled incorrectly or uses the wrong case, for example), or the code asks for the wrong thing (you are using mixed versions of the libraries?some from one version of the product, others from another version). Numerous kinds of coding and build errors can cause LNK2020. Several specific causes are listed below, and some have links to more detailed explanations. …… 《面向对象程序设计》 中国科学技术大学计算机系 马建辉 29 模板中的常量 模板参数并不局限于类定义的类。oop(c第12章模板最终版ppt-第12章模板template
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。