'operator new':重新定义,不同的链接(在重新定义的 new 运算符上使用 _dllspec)

发布于 2024-08-07 23:35:51 字数 639 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何以心动 2024-08-14 23:35:51

C++运行时本身提供了一个operator new,它没有被标记为DECLSPECCORE——因此“不同的链接”,原始的不是从另一个模块导入的。如果您打算覆盖operator new,它应该具有与以前相同的链接。

The C++ runtime itself provides an operator new, which is not marked as DECLSPECCORE -- hence "different linkage", the original was not imported from another module. If you intend to override operator new, it should have the same linkage as before.

马蹄踏│碎落叶 2024-08-14 23:35:51

一个可能的解决方案是将 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.

绻影浮沉 2024-08-14 23:35:51

如果您有两个重载新运算符的原型,则必须将两者都导出。希望这是你的问题。

If you have two two prototypes of overloading the new operator you must export both. Hopefulyl that is your problem.

忆梦 2024-08-14 23:35:51

您的代码表示您希望将 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文