【发布时间】:2013-08-04 16:18:31
【问题描述】:
当我尝试覆盖 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:\用户\文档\视觉工作室 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;
}
有人可以帮忙吗?
【问题讨论】:
标签: c++ visual-studio visual-c++ visual-studio-2012