免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6084 | 回复: 2
打印 上一主题 下一主题

求教高手no instance of overloaded 【new】错误如何解决 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-08-13 17:31 |只看该作者 |倒序浏览
--demo.h
#ifndef _DEMO_H
#define _DEMO_H
#endif
class TestObj
{
public:       
        char z[15];
        TestObj():z("" {}
        void* operator new( size_t nSize )
{
        return :perator new( nSize );
}
        virtual ~TestObj() {};
};
---demo.cpp
#include <stdio.h>
#include "demo.h"
using namespace std;

#if defined( MEM_DEBUG )
#include "MemRecord.h"
#define new new( __FILE__, __LINE__ )
#endif
/******************MemRecord.cpp里对NEW进行了重载,如下****************************
void* operator new( size_t nSize, char* pszFileName, int nLineNum )
{
        MemOperation record;
        void *pResult;
       
        pResult = :perator new( nSize );
       
        if ( pResult )
        {
                // record the alloc memory
                strncpy( record.Filename, pszFileName, FILENAME_LENGTH - 1 );
                record.Filename[ FILENAME_LENGTH - 1 ] = '\0';
                record.LineNum = nLineNum;
                record.AllocSize = nSize;
                record.OperationType = SINGLE_NEW;
                record.errCode = 0;
                record.pBuffer = pResult;
                appMemory.Insert( pResult, &record );
                appMemory.InsertDB( &record );
        }
       
        return pResult;
}
******************************************************************************/

int main()
{
        TestObj* pob = new TestObj;       
        return 0;
}
-------
编译出错
"demo.cpp", line 39: error #2384: no instance of overloaded
          "TestObj:perator new" matches the argument list
            argument types are: (unsigned long, const char [9], int)
        TestObj* pob = new TestObj;     
                       ^

1 error detected in the compilation of "demo.cpp".

论坛徽章:
0
2 [报告]
发表于 2010-08-13 17:39 |只看该作者
为了检测内存泄露,重载了NEW,记录下他的文件名的行数;
但是放到工程用发现工程也有对NEW的重载,比如demo.h这个列子,由于工程文件不能改变
所以要怎么才能把让重载的NEW和工程里重载的new不冲突呢

论坛徽章:
0
3 [报告]
发表于 2010-08-13 17:41 |只看该作者
---demo.cpp
#include <stdio.h>
#include "demo.h"
using namespace std;

#if defined( MEM_DEBUG )
#include "MemRecord.h"
#define new new( __FILE__, __LINE__ )
#endif

int main()
{
        TestObj* pob = new TestObj;        
        return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP