首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

模版种做基类,编译失败:无法解析的标记

2013-01-23 
模版类做基类,编译失败:无法解析的标记文件如下:CachedObj.h:#ifndef CACHED_OBJ_H#define CACHED_OBJ_H#i

模版类做基类,编译失败:无法解析的标记
文件如下:
CachedObj.h:


#ifndef CACHED_OBJ_H
#define CACHED_OBJ_H
#include <string>
using namespace std;
template <typename T>
class CachedObj{
private:
static T* freeSpace;
static allocator<T> memAlloc;
static void addToFreeList(T*);
static const size_t chunk = 0;
protected:
T *next;
public:
void* operator new(size_t);
void operator delete(void*,size_t);
virtual ~CachedObj();
};
template <typename T>
void CachedObj<T>::addToFreeList(T *t){
t->CachedObj<T>::next = freeSpace;
freeSpace = t;
};

template <typename T>
void* CachedObj<T>::operator new(size_t size){
if(size!=sizeof(T))//不可适用于继承
{
throw runtime_error("CachedObj:wrong size of objsize");
}
if(!freeSpace){
T* arr = memAlloc.allocate(chunk);
for(size_t i=0;i!=chunk;++i){
addToFreeList(&arr[i]);
}
}
T *p = freeSpace;
freeSpace = freeSpace->CachedObj<T>::next;
return p;
};
template <typename T>
void CachedObj<T>::operator delete(void* p,size_t size){
if(p)
addToFreeList((T*)p);
};

/*init static members*/
template<typename T>
T* CachedObj<T>::freeSpace = 0;
template<typename T>
std::allocator<T> CachedObj<T>::memAlloc;

class TestCached:public CachedObj<TestCached>{
string me;
public:
TestCached():me(string("")){};
TestCached(string &str):me(str){};
};

#endif


CPPEx.cpp:

#include <string>
#include "CachedObj.h"
using namespace std;

int main(){
int times = 0;
string x = "xc";
TestCached *p;
while (times!=100000){
p = new TestCached(x);
delete p;
}

system("pause");
return 1;
}


>CPPEx.obj : error LNK2028: 无法解析的标记(0A000462) "public: virtual __thiscall CachedObj<class TestCached>::~CachedObj<class TestCached>(void)" (??1?$CachedObj@VTestCached@@@@$$FUAE@XZ),该标记在函数 "public: __thiscall TestCached::TestCached(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (??0TestCached@@$$FQAE@AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) 中被引用
1>CPPEx.obj : error LNK2019: 无法解析的外部符号 "public: virtual __thiscall CachedObj<class TestCached>::~CachedObj<class TestCached>(void)" (??1?$CachedObj@VTestCached@@@@$$FUAE@XZ),该符号在函数 "public: __thiscall TestCached::TestCached(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (??0TestCached@@$$FQAE@AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) 中被引用 无法解析的标记 无法解析的外部符号 模版类
[解决办法]
CachedObj析构 函数没实现啊

热点排行