/* memory allocation class: * Pre-allocates objects and maintains a freelist * of objects that are unused * When an object is freed, it is put back on the freelist * The memory is only returned when the program exists */
template <typename T> class CachedObj { public: void* operator new (std::size_t); void operator delete (void*, std::size_t); virtual ~CachedObj() { } protected: T *next; private: static void add_to_freelist(T*); static std::allocator <T> alloc_mem; static T *freeList; static const std::size_t chunk; };
为什么编译时提示: --------------------Configuration: cachedobj - Win32 Debug-------------------- Compiling... cachedobj.cpp C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(18) : error C2039: 'size_t' : is not a member of 'std' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(28) : see reference to class template instantiation 'CachedObj <T>' being compiled C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(19) : error C2039: 'size_t' : is not a member of 'std' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(28) : see reference to class template instantiation 'CachedObj <T>' being compiled C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(27) : error C2039: 'size_t' : is not a member of 'std' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(28) : see reference to class template instantiation 'CachedObj <T>' being compiled C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(18) : error C2039: 'size_t' : is not a member of 'std' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(76) : see reference to class template instantiation 'CachedObj <class TestCachedObj>' being compiled C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(19) : error C2039: 'size_t' : is not a member of 'std' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(76) : see reference to class template instantiation 'CachedObj <class TestCachedObj>' being compiled C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(27) : error C2039: 'size_t' : is not a member of 'std' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(76) : see reference to class template instantiation 'CachedObj <class TestCachedObj>' being compiled C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(94) : warning C4291: 'void *__cdecl CachedObj <class TestCachedObj>::operator new(unsigned int)' : no matching operator delete found; memory will not be freed if initialization throws an exception C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(18) : see declaration of 'new' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(99) : warning C4291: 'void *__cdecl CachedObj <class TestCachedObj>::operator new(unsigned int)' : no matching operator delete found; memory will not be freed if initialization throws an exception C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(18) : see declaration of 'new' 执行 cl.exe 时出错.
我在VC6.0中void* operator new (std::size_t); 中的std::去掉再编译那错误提示中就没有这一行了 --------------------Configuration: cachedobj - Win32 Debug-------------------- Compiling... cachedobj.cpp C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(19) : error C2039: 'size_t' : is not a member of 'std' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(28) : see reference to class template instantiation 'CachedObj <T>' being compiled C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(27) : error C2039: 'size_t' : is not a member of 'std' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(28) : see reference to class template instantiation 'CachedObj <T>' being compiled C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(19) : error C2039: 'size_t' : is not a member of 'std' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(76) : see reference to class template instantiation 'CachedObj <class TestCachedObj>' being compiled C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(27) : error C2039: 'size_t' : is not a member of 'std' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(76) : see reference to class template instantiation 'CachedObj <class TestCachedObj>' being compiled C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(94) : warning C4291: 'void *__cdecl CachedObj <class TestCachedObj>::operator new(unsigned int)' : no matching operator delete found; memory will not be freed if initialization throws an exception C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(18) : see declaration of 'new' C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(99) : warning C4291: 'void *__cdecl CachedObj <class TestCachedObj>::operator new(unsigned int)' : no matching operator delete found; memory will not be freed if initialization throws an exception C:\Documents and Settings\wjl821\桌面\c code\COM\cachedobj\cachedobj.cpp(18) : see declaration of 'new' 执行 cl.exe 时出错.