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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
The Last Watchdog
The Last Watchdog
Project Zero
Project Zero
T
Threatpost
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
B
Blog
The GitHub Blog
The GitHub Blog
F
Full Disclosure
MyScale Blog
MyScale Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Y
Y Combinator Blog
GbyAI
GbyAI
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
NISL@THU
NISL@THU
爱范儿
爱范儿
Spread Privacy
Spread Privacy
F
Fortinet All Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hacker News - Newest:
Hacker News - Newest: "LLM"
Simon Willison's Weblog
Simon Willison's Weblog
S
Security @ Cisco Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
PCI Perspectives
PCI Perspectives
V
V2EX
人人都是产品经理
人人都是产品经理
W
WeLiveSecurity
IT之家
IT之家
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
博客园_首页
Forbes - Security
Forbes - Security
Cloudbric
Cloudbric
有赞技术团队
有赞技术团队
Application and Cybersecurity Blog
Application and Cybersecurity Blog
B
Blog RSS Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Palo Alto Networks Blog
The Register - Security
The Register - Security
Google DeepMind News
Google DeepMind News
Hacker News: Ask HN
Hacker News: Ask HN
腾讯CDC
T
Threat Research - Cisco Blogs
Cyberwarzone
Cyberwarzone

博客园 - 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();
}