'operator new':重新定义,不同的链接(在重新定义的 new 运算符上使用 _dllspec)
我在 new 的调试版本上使用 __declspec(dllimport/export) :
#ifdef _DEBUG
DECLSPECCORE extern void* operator new(unsigned int size, const char* file, int line);
extern void* operator new[](unsigned int size, const char* file, int line);
extern void operator delete(void* address, const char* file, int line);
extern void operator delete[](void* address, const char* file, int line);
extern void Delete(void* address);
#define LUDO_NEW new(__FILE__, __LINE__)
#define LUDO_DELETE delete
#endif
这导致我得到
错误 C2375:“新运算符”: 重新定义;不同的链接。
这是为什么?如何解决?这是我现在正在编译的唯一项目。
I am using __declspec(dllimport/export) on a debug version of new as such:
#ifdef _DEBUG
DECLSPECCORE extern void* operator new(unsigned int size, const char* file, int line);
extern void* operator new[](unsigned int size, const char* file, int line);
extern void operator delete(void* address, const char* file, int line);
extern void operator delete[](void* address, const char* file, int line);
extern void Delete(void* address);
#define LUDO_NEW new(__FILE__, __LINE__)
#define LUDO_DELETE delete
#endif
This is causing me to get
error C2375: 'operator new':
redefinition; different linkage.
Why is this and how can you fix it? This is the only project that I am compiling right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++运行时本身提供了一个
operator new
,它没有被标记为DECLSPECCORE
——因此“不同的链接”,原始的不是从另一个模块导入的。如果您打算覆盖operator new
,它应该具有与以前相同的链接。The C++ runtime itself provides an
operator new
, which is not marked asDECLSPECCORE
-- hence "different linkage", the original was not imported from another module. If you intend to overrideoperator new
, it should have the same linkage as before.一个可能的解决方案是将 new 和 delete 运算符移至其自己的命名空间。
该命名空间中的所有类型都应该使用该命名空间的 new 和 delete 运算符。
A possible solution is to move the new and delete operator to it's own namespace.
all types in that namespace should than use the new and delete operator of that namespace.
如果您有两个重载新运算符的原型,则必须将两者都导出。希望这是你的问题。
If you have two two prototypes of overloading the new operator you must export both. Hopefulyl that is your problem.
您的代码表示您希望将 C++ 的“operator new”导出为可从 DLL 外部调用的函数。假设这是可能的(可能不是):您确定这就是您想要做的吗?
Your code is saying that you want C++'s "operator new" to be exported as a function callable from outside the DLL. Assuming that's even possible (probably not): Are you sure that's what you want to do?