﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-早上晒晒太阳</title><link>http://www.cppblog.com/IwillBoost/</link><description>My way ahead is a long long one oh&amp;nbsp
I will seek my beauty high and low</description><language>zh-cn</language><lastBuildDate>Tue, 14 Apr 2026 23:06:09 GMT</lastBuildDate><pubDate>Tue, 14 Apr 2026 23:06:09 GMT</pubDate><ttl>60</ttl><item><title>VC的模板写法</title><link>http://www.cppblog.com/IwillBoost/archive/2006/11/29/15790.html</link><dc:creator>结斌</dc:creator><author>结斌</author><pubDate>Wed, 29 Nov 2006 12:24:00 GMT</pubDate><guid>http://www.cppblog.com/IwillBoost/archive/2006/11/29/15790.html</guid><wfw:comment>http://www.cppblog.com/IwillBoost/comments/15790.html</wfw:comment><comments>http://www.cppblog.com/IwillBoost/archive/2006/11/29/15790.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/IwillBoost/comments/commentRss/15790.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/IwillBoost/services/trackbacks/15790.html</trackback:ping><description><![CDATA[　　模板在没有实例化之前是无法编译的，因为没有使用类型实例化的模板是不能生成代码的。所以一般处理模板的方法是，将模板的声明和定义都放到一个.h文件之中，或者使用export关键字，但是后者在很多C++编译器实现（如：VC）中不被支持.<br />　　如果说硬要把声明和定义分开来写的，也是可以的，不过用起来就不好了！例如，要做一个Stack，会在main中调用Stack&lt;int&gt; s;这样的语句，那么就必须在Stack.cpp文件的后面加上一句template class Stack&lt;int&gt;;<br /><hr /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #000000">template </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> T</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Stack<br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">    Stack();<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">    </span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">Stack();<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> pop();<br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/dot.gif" /><img src="http://www.cppblog.com/images/dot.gif" /></span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">};</span></div><hr /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Stack.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">template </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> T</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">Stack</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">T</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::Stack(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">{}<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">template </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> T</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">Stack</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">T</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">Stack(</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">{}<br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">template </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> t</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> Stack</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">T</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">::pop()<br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">{<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/dot.gif" /><img src="http://www.cppblog.com/images/dot.gif" /></span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">}<br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">template </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Stack</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">;</span></div><br /><hr />
　　可以看出，用户要用其它类型的Stack的话就必须在Stack.cpp文件中加入具体的类型.不然就会出现类似这样的错误提示：<br />　　error LNK2019: 无法解析的外部符号 "public: __thiscall Stack&lt;int&gt;::~Stack&lt;int&gt;(void)" (??1?$Stack＠H＠＠QAE＠XZ)，该符号在函数 _main 中被引用<br />　　所以，还是所声明与定义都会在.h文件中吧.<img src ="http://www.cppblog.com/IwillBoost/aggbug/15790.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/IwillBoost/" target="_blank">结斌</a> 2006-11-29 20:24 <a href="http://www.cppblog.com/IwillBoost/archive/2006/11/29/15790.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>现在的研究生</title><link>http://www.cppblog.com/IwillBoost/archive/2006/11/29/15769.html</link><dc:creator>结斌</dc:creator><author>结斌</author><pubDate>Wed, 29 Nov 2006 02:43:00 GMT</pubDate><guid>http://www.cppblog.com/IwillBoost/archive/2006/11/29/15769.html</guid><wfw:comment>http://www.cppblog.com/IwillBoost/comments/15769.html</wfw:comment><comments>http://www.cppblog.com/IwillBoost/archive/2006/11/29/15769.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/IwillBoost/comments/commentRss/15769.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/IwillBoost/services/trackbacks/15769.html</trackback:ping><description><![CDATA[
		<p>        昨天，去听报告了！这是学校的规定：新生必须听四次研究生处组织的报告！<br />        去的人还是比较多的，我坐在后面！还没开始，左邻右里就开始发话了，在那里狂吹！这没什么，反正还没有开始嘛，说她们也不好意思！我就安静的坐好！<br />　　好了，开始了！这次做讲座的是生命科学院的博导，内容是：从DNA的发展，看现代生物工程！是一个比较有趣的话题！我周边的研究生，一直没有停止她们的悬河，还是在决堤，滔滔不绝！我也算是一支独秀了（没话可说）！讲座是一个半小时，她们也就在那狂轰了一个半小时！<br /> 　　最后了，王老师说到了提问的环节了！后面一个女生出了这么一句：<font color="#ff0000">那个老师怎么还在说话啊，浪费人时间啊！<br /></font>　　现在的研究生的素质怎么这样子啊！？你要听不听，开＂小＂差还算次之，居然发出这种言论，可耻！</p>
<img src ="http://www.cppblog.com/IwillBoost/aggbug/15769.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/IwillBoost/" target="_blank">结斌</a> 2006-11-29 10:43 <a href="http://www.cppblog.com/IwillBoost/archive/2006/11/29/15769.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>