﻿<?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++博客-guaguaman</title><link>http://www.cppblog.com/guaguaman/</link><description /><language>zh-cn</language><lastBuildDate>Fri, 17 Apr 2026 03:21:31 GMT</lastBuildDate><pubDate>Fri, 17 Apr 2026 03:21:31 GMT</pubDate><ttl>60</ttl><item><title>C++模板类与LNK2005</title><link>http://www.cppblog.com/guaguaman/archive/2012/05/25/176143.html</link><dc:creator>赵氏呱呱儿</dc:creator><author>赵氏呱呱儿</author><pubDate>Fri, 25 May 2012 05:40:00 GMT</pubDate><guid>http://www.cppblog.com/guaguaman/archive/2012/05/25/176143.html</guid><wfw:comment>http://www.cppblog.com/guaguaman/comments/176143.html</wfw:comment><comments>http://www.cppblog.com/guaguaman/archive/2012/05/25/176143.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/guaguaman/comments/commentRss/176143.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/guaguaman/services/trackbacks/176143.html</trackback:ping><description><![CDATA[目前大部分的编译器都不支持C++模板类的声明与定义分离编译，因此模板类的声明和定义都要在同一个头文件中给出。<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000; ">//</span><span style="color: #008000; ">----1.h-----</span><span style="color: #008000; ">//<br /></span>#pragma&nbsp;once<br />template&lt;typename&nbsp;_T&gt;<br /><span style="color: #0000FF; ">class</span>&nbsp;A<br />{<br /><span style="color: #0000FF; ">public</span>:<br />&nbsp;&nbsp;&nbsp;A():a(_T(0)),b(_T(0)){}<br />&nbsp;&nbsp;&nbsp;A(_T&nbsp;ta,_T&nbsp;tb):a(ta),b(tb){}<br />&nbsp;&nbsp;&nbsp;~A(){}<br /><span style="color: #0000FF; ">public</span>:<br />&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">const</span>&nbsp;_T&nbsp;sum()&nbsp;<span style="color: #0000FF; ">const</span>{&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;a+b;&nbsp;};<br /><span style="color: #0000FF; ">private</span>:<br />&nbsp;&nbsp;&nbsp;_T&nbsp;a;<br />&nbsp;&nbsp;&nbsp;_T&nbsp;b;<br />};<br /><span style="color: #008000; ">//</span><span style="color: #008000; ">顶层函数</span><span style="color: #008000; "><br /></span>template&lt;typename&nbsp;_T&gt;<br />A&lt;_T&gt;&nbsp;<span style="color: #0000FF; ">operator</span>&nbsp;+&nbsp;(<span style="color: #0000FF; ">const</span>&nbsp;A&lt;_T&gt;&nbsp;&amp;al,<span style="color: #0000FF; ">const</span>&nbsp;A&lt;_T&gt;&nbsp;&amp;ar)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;A&lt;_T&gt;&nbsp;ta(al.sum(),ar.sum());<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;ta;<br />}</div>如果将声明放在1.h，定义放在1.cpp，如果按照正常的编程方式，在mian.cpp中加入#include "1.h"，则编译会出现如下错误：<br /><div><div>1&gt;main.obj : error LNK2019: 无法解析的外部符号 "class A&lt;int&gt; __cdecl operator+&lt;int&gt;(class A&lt;int&gt; const &amp;,class A&lt;int&gt; const &amp;)" (??$?HH@@YA?AV?$A@H@@ABV0@0@Z)，该符号在函数 _main 中被引用</div><div>1&gt;E:\C++\test\Debug\test.exe : fatal error LNK1120: 1 个无法解析的外部命令<br />//-----------------分割线-------------------//<br />如果在1.h头文件中出现了非模板函数，譬如说<br /><span style="background-color: #eeeeee; font-size: 13px; color: #008000; ">//</span><span style="background-color: #eeeeee; font-size: 13px; color: #008000; ">-------1.h---------</span><span style="background-color: #eeeeee; font-size: 13px; color: #008000; ">//</span><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all">A&lt;<span style="color: #0000FF; ">double</span>&gt;&nbsp;<span style="color: #0000FF; ">operator</span>&nbsp;+&nbsp;(<span style="color: #0000FF; ">const</span>&nbsp;A&lt;<span style="color: #0000FF; ">double</span>&gt;&nbsp;&amp;al,<span style="color: #0000FF; ">const</span>&nbsp;A&lt;<span style="color: #0000FF; ">int</span>&gt;&nbsp;&amp;ar)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;A&lt;<span style="color: #0000FF; ">double</span>&gt;&nbsp;ta(al.sum(),(<span style="color: #0000FF; ">double</span>)(ar.sum()));<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;ta;<br />}</div>那么这个非模板函数最好声明和定义分离，即将非模板函数定义写在1.cpp中，否则在多个CPP文件同时包含1.cpp时会出现LN2005错误：<br /><div><div>1&gt;1.obj : error LNK2005: "class A&lt;double&gt; __cdecl operator+(class A&lt;double&gt; const &amp;,class A&lt;int&gt; const &amp;)" (??H@YA?AV?$A@N@@ABV0@ABV?$A@H@@@Z) 已经在 main.obj 中定义</div><div>1&gt;E:\C++\test\Debug\test.exe : fatal error LNK1169: 找到一个或多个多重定义的符号</div></div>//-----------------分割线------------------//<br />总结：模板类的声明和定义在同一个头文件中，非模板类或函数的声明和定义分别写在头文件和cpp文件中。</div></div><img src ="http://www.cppblog.com/guaguaman/aggbug/176143.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/guaguaman/" target="_blank">赵氏呱呱儿</a> 2012-05-25 13:40 <a href="http://www.cppblog.com/guaguaman/archive/2012/05/25/176143.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Windows系统下静态链接 IMSL CNL 函数库 — VS2008</title><link>http://www.cppblog.com/guaguaman/archive/2012/05/18/175319.html</link><dc:creator>赵氏呱呱儿</dc:creator><author>赵氏呱呱儿</author><pubDate>Fri, 18 May 2012 09:23:00 GMT</pubDate><guid>http://www.cppblog.com/guaguaman/archive/2012/05/18/175319.html</guid><wfw:comment>http://www.cppblog.com/guaguaman/comments/175319.html</wfw:comment><comments>http://www.cppblog.com/guaguaman/archive/2012/05/18/175319.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/guaguaman/comments/commentRss/175319.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/guaguaman/services/trackbacks/175319.html</trackback:ping><description><![CDATA[版本：CNL700/winms800i32<br />步骤1：打开VS2008，打开或新建一个项目<br />步骤2：项目 &#8212; 属性（弹出属性页）&#8212; 配置属性 &#8212; C/C++ &#8212; 常规 &#8212; 附加包含目录<br /><p>添加：<span>&lt;VNI_DIR&gt;\imsl\cnl700\&lt;ENV&gt;\include<br /></span></p><p><span>步骤3：</span>项目 &#8212; 属性（弹出属性页）&#8212; 配置属性 &#8212; C/C++ &#8212; 预处理器 &#8212; 预处理器定义<br />添加：<span>IMSL_STATIC<br />步骤4：</span>项目 &#8212; 属性（弹出属性页）&#8212; 配置属性 &#8212; 链接器 &#8212; 常规 &#8212; 链接器依赖项<br />添加：<span>&lt;VNI_DIR&gt;\imsl\cnl</span><span>700\&lt;ENV&gt;\lib<br /></span></p><div>步骤5：项目 &#8212; 属性（弹出属性页）&#8212; 配置属性 &#8212; 链接器 &#8212; 输入 &#8212; 附加依赖项<br />添加：<br /><span>imslcmath_imsl.lib&nbsp;</span></div><div><span>imslcstat_imsl.lib&nbsp;</span><wbr><br /><span>lmgr_md.lib&nbsp;</span><wbr><br /><span>libcrvs_md.lib</span><br /><span>libsb_md.lib&nbsp;</span><wbr><br /><span>libFNPload.lib&nbsp;</span><wbr><br /><span>netapi32.lib&nbsp;</span><wbr><br /><span>advapi32.lib&nbsp;</span><wbr><br /><span>comctl32.lib&nbsp;</span><wbr><br /><span>wsock32.lib&nbsp;</span><wbr><br /><span>oldnames.lib&nbsp;<br /></span><div>步骤6：项目 &#8212; 属性（弹出属性页）&#8212; 配置属性 &#8212; 链接器 &#8212; 输入 &#8212; &nbsp;忽略特定库<br />添加：libcmt.lib<br />步骤7：点击确定</div></div><div>&nbsp;</div><p>&nbsp;</p><div>&nbsp;</div><span><br /></span><p>&nbsp;</p><img src ="http://www.cppblog.com/guaguaman/aggbug/175319.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/guaguaman/" target="_blank">赵氏呱呱儿</a> 2012-05-18 17:23 <a href="http://www.cppblog.com/guaguaman/archive/2012/05/18/175319.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）vector中erase用法注意事项</title><link>http://www.cppblog.com/guaguaman/archive/2010/11/10/133240.html</link><dc:creator>赵氏呱呱儿</dc:creator><author>赵氏呱呱儿</author><pubDate>Wed, 10 Nov 2010 14:01:00 GMT</pubDate><guid>http://www.cppblog.com/guaguaman/archive/2010/11/10/133240.html</guid><wfw:comment>http://www.cppblog.com/guaguaman/comments/133240.html</wfw:comment><comments>http://www.cppblog.com/guaguaman/archive/2010/11/10/133240.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/guaguaman/comments/commentRss/133240.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/guaguaman/services/trackbacks/133240.html</trackback:ping><description><![CDATA[<p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">以前就发现了vector中的erase方法有些诡异(^_^)，稍不注意，就会出错。今天又一次遇到了，就索性总结一下，尤其是在循环体中用erase时，由于vector.begin() 和vector.end()是变化的，因此就引入了错误的可能性。</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">vector&lt;int&gt; veci;<br style="line-height: normal; ">veci.push_back(1);<br style="line-height: normal; ">veci.push_back(2);<br style="line-height: normal; ">veci.push_back(3);<br style="line-height: normal; ">veci.push_back(4);<br style="line-height: normal; ">veci.push_back(5);<br style="line-height: normal; ">veci.push_back(3);<br style="line-height: normal; ">veci.push_back(2);<br style="line-height: normal; ">veci.push_back(3);</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">for(vector&lt;int&gt;::iterator iter=veci.begin(); iter!=veci.end(); iter++)<br style="line-height: normal; ">{<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( *iter == 3)<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; veci.erase(iter);<br style="line-height: normal; ">}</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">乍一看这段代码，很正常。其实这里面隐藏着一个很严重的错误：当veci.erase(iter)之后，iter就变成了一个野指针，对一个野指针进行 iter++ 是肯定会出错的。</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">查看MSDN，对于erase的返回值是这样描述的：An iterator that designates the first element remaining beyond any elements removed, or a pointer to the end of the vector if no such element exists，于是改代码：</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">for(vector&lt;int&gt;::iterator iter=veci.begin(); iter!=veci.end(); iter++)<br style="line-height: normal; ">{<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( *iter == 3)<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; iter = veci.erase(iter);<br style="line-height: normal; ">}</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">这段代码也是错误的：1）无法删除两个连续的"3"； 2）当3位于vector最后位置的时候，也会出错（在veci.end()上执行 ++ 操作）</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">正确的代码应该为：</p><p style="line-height: normal; color: rgb(51, 102, 102); font-family: Arial; font-size: 14px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">for(vector&lt;int&gt;::iterator iter=veci.begin(); iter!=veci.end(); )<br style="line-height: normal; ">{<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp; if( *iter == 3)<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iter = veci.erase(iter);<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iter ++ ;<br style="line-height: normal; ">}</p>
<img src ="http://www.cppblog.com/guaguaman/aggbug/133240.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/guaguaman/" target="_blank">赵氏呱呱儿</a> 2010-11-10 22:01 <a href="http://www.cppblog.com/guaguaman/archive/2010/11/10/133240.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>