当我尝试覆盖 VS2012 中的默认内存分配函数时,出现以下链接器错误:
1>Main.obj:错误 LNK2005:“void * __cdecl operator new(unsigned int)”(??2@YAPAXI@Z) 已在 MSVCRTD.lib(MSVCR110D.dll) 中定义 1>Main.obj:错误 LNK2005:“ void __cdecl operator delete(void *)" (??3@YAXPAX@Z) 已在 MSVCRTD.lib(MSVCR110D.dll) 1>c:\users\documents\visual studio 2012\Projects\CustomMemoryAllocator\Debug\CustomMemoryAllocator 中定义。 exe:致命错误 LNK1169:找到一个或多个多重定义的符号
这是我的代码(我没有智能感知错误):
#include <iostream>
using namespace std;
void *operator new(size_t size){
if(void *mem = malloc(size)){
cout << "allocated memory" << endl;
return mem;
}
else{
throw bad_alloc();
}
}
void operator delete(void* mem) throw() {
cout << "deleting" << endl;
free(mem);
}
int main(){
cout << "test";
int* a = new int(4);
delete a;
int b = 0;
cin >> b;
}
有人可以帮忙吗?