为什么放在一个.cpp文件中可以,放在不同的文件却不可以?

lp310018931 2015-01-17 10:25:11
#include <iostream>

//#include "CSingleton.h"

using namespace std;

//CSingleton.h
template <typename T>
class CSingleton
{
public:
static T *Instance();

private:
CSingleton(); // 默认的构造函数
CSingleton(const CSingleton<T> &other); // 拷贝构造函数
CSingleton &operator=(const CSingleton<T> &other); // 赋值函数
~CSingleton(); // 析构函数

static T *m_ptInstance;
};

//CSingleton.cpp
template <typename T>
T *CSingleton<T>::m_ptInstance = NULL;

template <typename T>
T *CSingleton<T>::Instance()
{
if (NULL == m_ptInstance)
{
m_ptInstance = new T();
}

return m_ptInstance;
}

//main.cpp
/* 测试类 */
class CTest
{
public:
CTest()
{
cout<<"i: "<<i<<endl;
++i;
}

~CTest()
{
}

private:
static int i;
};

int CTest::i = 0;

int main(void)
{
CTest *pTest = CSingleton<CTest>::Instance();
CTest *pTest2 = CSingleton<CTest>::Instance();

getchar();
return 0;
}

为什么放在一个.cpp文件中可以,放在不同的文件却不可以?
...全文
252 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lp310018931 2015-01-18
  • 打赏
  • 举报
回复
引用 1 楼 pengzhixi 的回复:
模板不可分离编译
多谢
lp310018931 2015-01-18
  • 打赏
  • 举报
回复
引用 5 楼 mujiok2003 的回复:
1. rename Singleton.cpp to Singleton.hpp

//Singleton.hpp
#include "Singleton.h"
.....
2.
//test.h
#include "Singleton.h"
class CTest
{
public:
CTest()
{
cout<<"i: "<<i<<endl;
++i;
}

~CTest()
{
}

private:
static int i;
};
//prevent the automatic instantiation of members.
extern template  class Singleton<CTest>; 
/test.cpp
#inlcude "test.h"
#include "Singleton.hpp"

int CTest::i = 0;
template  class Singleton<CTest>; //Explicit Instantiation
3. in main.cpp or other src files that will use Singleton<CTest>

#include "CTest.h"
这样可加速编译, 只在CTest.cpp中实例化模板类Singleton<CTest>,main.cpp等用户只检查语法错误, 没有模板膨胀的问题.
多谢
lp310018931 2015-01-18
  • 打赏
  • 举报
回复
引用 楼主 lp310018931 的回复:
#include <iostream> //#include "CSingleton.h" using namespace std; //CSingleton.h template <typename T> class CSingleton { public: static T *Instance(); private: CSingleton(); // 默认的构造函数 CSingleton(const CSingleton<T> &other); // 拷贝构造函数 CSingleton &operator=(const CSingleton<T> &other); // 赋值函数 ~CSingleton(); // 析构函数 static T *m_ptInstance; }; //CSingleton.cpp template <typename T> T *CSingleton<T>::m_ptInstance = NULL; template <typename T> T *CSingleton<T>::Instance() { if (NULL == m_ptInstance) { m_ptInstance = new T(); } return m_ptInstance; } //main.cpp /* 测试类 */ class CTest { public: CTest() { cout<<"i: "<<i<<endl; ++i; } ~CTest() { } private: static int i; }; int CTest::i = 0; int main(void) { CTest *pTest = CSingleton<CTest>::Instance(); CTest *pTest2 = CSingleton<CTest>::Instance(); getchar(); return 0; } 为什么放在一个.cpp文件中可以,放在不同的文件却不可以?
引用 3 楼 zmlovelx 的回复:
多数编译器不支持模板分离编译。 放.h中好了。
多谢
pengzhixi 2015-01-17
  • 打赏
  • 举报
回复
模板不可分离编译
yangyunzhao 2015-01-17
  • 打赏
  • 举报
回复
引用 2 楼 tixisong 的回复:
可以用.h .cpp分开写,但.h 末尾用#include "xx.cpp" 包含
野狐禅
mujiok2003 2015-01-17
  • 打赏
  • 举报
回复
extern template 已经在vs .net 2003中已经支持。
mujiok2003 2015-01-17
  • 打赏
  • 举报
回复
1. rename Singleton.cpp to Singleton.hpp

//Singleton.hpp
#include "Singleton.h"
.....
2.
//test.h
#include "Singleton.h"
class CTest
{
public:
CTest()
{
cout<<"i: "<<i<<endl;
++i;
}

~CTest()
{
}

private:
static int i;
};
//prevent the automatic instantiation of members.
extern template  class Singleton<CTest>; 
/test.cpp
#inlcude "test.h"
#include "Singleton.hpp"

int CTest::i = 0;
template  class Singleton<CTest>; //Explicit Instantiation
3. in main.cpp or other src files that will use Singleton<CTest>

#include "CTest.h"
这样可加速编译, 只在CTest.cpp中实例化模板类Singleton<CTest>,main.cpp等用户只检查语法错误, 没有模板膨胀的问题.
勤奋的小游侠 2015-01-17
  • 打赏
  • 举报
回复
模板不可分离编译
帅得不敢出门 2015-01-17
  • 打赏
  • 举报
回复
多数编译器不支持模板分离编译。 放.h中好了。
tixisong 2015-01-17
  • 打赏
  • 举报
回复
可以用.h .cpp分开写,但.h 末尾用#include "xx.cpp" 包含

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧