首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 关于size_t [已结贴,结贴人:kgduwu]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • kgduwu
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 揭贴率:
    发表于:2008-07-05 23:21:45 楼主
    #include <iostream>
    #include <stdexcept>
    #include <cstddef>
    #include <memory>

    #define _DEBUG_

    /* 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 时出错.

    cachedobj.obj - 1 error(s), 0 warning(s)
    120  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • steedhorse
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 2

      3

      4

    发表于:2008-07-05 23:36:291楼 得分:5
    那就把
    static const std::size_t chunk;
    改成
    static const size_t chunk;
    试试看。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • kgduwu
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-05 23:55:102楼 得分:0
    我在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 时出错.

    cachedobj.obj - 1 error(s), 0 warning(s)
    可为什么要这样呢,为什么不能加上std::呢?
    感谢晨星老大指点!!!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • steedhorse
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 2

      3

      4

    发表于:2008-07-06 00:08:353楼 得分:5
    size_t究竟是不是定义在命名空间std中的,具体我也忘了,如果不加是对的,那就说明它不是定义在std中的。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • steedhorse
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 2

      3

      4

    发表于:2008-07-06 00:14:214楼 得分:20
    不对!
    是你用的编译器太高了吧。
    我用VS2005编译,std::size_t没有出现问题啊。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • kgduwu
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-06 00:17:535楼 得分:0
    我用的VC6.0,是不是版本太低了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • kgduwu
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-06 00:24:296楼 得分:0
    这源代码是从http://blog.csdn.net/armman/archive/2007/03/26/1542105.aspx上截下来的,它基本上是C++ primer中的内存分配器基类cachedobj定义与应用的代码!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • baihacker
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 3

    发表于:2008-07-06 08:35:047楼 得分:20
    楼主表用VC6了,换点高版本的.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • steedhorse
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 2

      3

      4

    发表于:2008-07-06 14:36:318楼 得分:0
    6.0确实太低了。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • k2eats
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-06 14:49:399楼 得分:10
    是 std::size_t
    VC6丢了吧
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • cppzhentan
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-06 15:03:4110楼 得分:5
    看来VC6要封尘了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • franckson
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-06 23:02:0311楼 得分:5
    换编译器
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • zhyinty
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-06 23:12:1712楼 得分:10
    size_t类型定义在cstddef头文件中,该文件是C标准库的头文件stddef.h的C++版本,他是一个与机器相关的unsigned类型,其大小足以保证存储内容中的对象的大小
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • visame
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-07 03:19:2513楼 得分:15
    size_t is defined in the standard header <cstddef>
    ptrdiff_t size_t都在#include <cstddef> 里面
    你已经包含了
    用GCC或者更高版本VC试一试
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • angelcm51
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-07 07:58:2614楼 得分:5
    引用 10 楼 cppzhentan 的回复:
    看来VC6要封尘了

    真的?。。。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • ForestDB
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-07 08:13:2815楼 得分:10
    个人感觉VC6对standard C++支持不怎么好,但用来开发native windows应用还是够了。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yuyunliuhen
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-07 08:48:4716楼 得分:10
    换个2008就好了,DEV下也没问题撒
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • nizhibuzhidaowoaini
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-07 09:01:0517楼 得分:0
    该回复于2008-07-07 09:19:01被版主删除
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • wangboqun99
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-07 15:52:0318楼 得分:0
    该回复于2008-07-07 09:19:01被版主删除
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • angelcm51
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-07-07 16:50:4419楼 得分:0
    引用 17 楼 nizhibuzhidaowoaini 的回复:
    该回复于2008-07-07 09:19:01被版主删除

    ...

    说了什么反动言论/-.-\
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved