惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - vibration

ATL3.0组件注册bug的解决方法 奇怪的引用错误及解决方法 OLE2T在VS2003中转换中文失败的问题及解决方法 标题栏按钮的WTL实现 AppBar的WTL实现 招聘C++和C#开发工程师 ATL组件中文路径注册问题(转载) 用窗口消息解决COM接口的多线程访问问题 用全局接口表实现COM接口在不同线程中的传递 MyMSN支持自定义内容了 自画菜单的WM_MEASUREITEM只会发送一次 悼念皮皮 上了点照片 一个测试记忆力的小游戏 老板该如何向核心员工许诺 ActiveScript SkinX界面换肤框架更新 反射获取定制Attribute Skin技术实现框架(完)
关于C++模板的连接问题 - vibration - 博客园
vibration · 2005-06-30 · via 博客园 - vibration

好久没写模板了,这次做一项目,发现一处非常适合使用模板应用,于是写了个模板类,大概类似于酱紫

//头文件
template <class TElement>
class TTTextT  
{
protected:
    vector
<TElement>    m_chVector;
public:
    
int    Count();
}
//CPP文件
#include "TTText.h"
template 
<class TElement>
int TTTextT<TElement>::Count()
{
    
return m_chVector.size();
}

当然还有许多其他代码啦,分成头文件和CPP文件,初步编译没什么问题。
可是在使用的时候,却产生了连接问题,说什么unresolved external symbol,函数找不到。忽然想起以前也碰到过类似问题,模板类的声名和实现如果放在两个文件里,就会发生这样的状况。当时没时间多想,把所有实现挪到头文件里就解决了。想看看人家的代码,不管是STL,ATL,WTL,都是所有内容全放头文件的。晕了,最后还是把所有代码挪到头文件,就OK了。
可是为什么呢?查MSDN,查google,都没有找到满意的答案。只看到一篇,说模板编译中不产生实际代码,只有加模板参数使用时才产生代码,因此如果分两个文件,连接时会出错,云云。不是特别明白,结论似乎是使用模板就应该把所有实现放在头文件中。看看STL/ATL/WTL,似乎也是。但问题是:既然这样,为什么允许下面这样的代码呢

template <class TElement>
int TTTextT<TElement>::Count()
{
    
return m_chVector.size();
}