关于 Visual C++ 里面的 warning C4661
该文章迁移自作者的旧博客站点。
源地址:http://fenying.blog.163.com/blog/static/102055993201581113852862/。
源地址:http://fenying.blog.163.com/blog/static/102055993201581113852862/。
最近写 C++ 类模板时遇到的一个 C4661 warning,记载于此。
问题代码如下:
/* test.h */
template <class T>
class Singleton {
protected:
static T* pInst;
}
class __declspec(dllexport) A: public Singleton<A> {
}
/* test.cc */
#include "test.h"
A* Singleton<A>::pInst = nullptr;
/* xxxx.cc */
#include "test.h"
// Do something...
出现如下警告:
1> xxxx.cc
1>test.h(42): warning C4661: “A* Singleton<A>::pInst”: 没有为显式模板实例化请求提供适当的定义
1> test.h(41): note: 参见“Singleton<A>::pInst”的声明
MSDN文档:https://msdn.microsoft.com/zh-cn/library/1k7k17za(v=vs.120).aspx
微软称是因为类模板成员只有声明没有定义,但是很显然我给了定义。
再看了下 SIngleton
和 A
的定义区别,发现 A
有 __declspec(dllexport)
,而 SIngleton
没有。给 Singleton
类加上 __declspec(dllexport)
,一切解决。
原因是,只给 A 加了 DLL 导出,却没有给 Singleton<A>
加 DLL 导出,所以 Singleton<A>::pInst
没有被导出。
comments powered by Disqus