+ -
当前位置:首页 → 问答吧 → 求教高手no instance of overloaded 【new】错误如何解决

求教高手no instance of overloaded 【new】错误如何解决

时间:2010-08-13

来源:互联网

--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".

作者: sotjimmy   发布时间: 2010-08-13

为了检测内存泄露,重载了NEW,记录下他的文件名的行数;
但是放到工程用发现工程也有对NEW的重载,比如demo.h这个列子,由于工程文件不能改变
所以要怎么才能把让重载的NEW和工程里重载的new不冲突呢

作者: sotjimmy   发布时间: 2010-08-13

---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;
}

作者: sotjimmy   发布时间: 2010-08-13