添加 #define new 时不是可识别的运算符或类型
我正在尝试解决我的程序的一部分,其中有一个#define new。一切正常,直到我尝试创建一个覆盖 new 运算符的类模板,这时我收到错误:
C:\Define_New_problem\main.cpp:18: error: expected type-specifier before 'dPushMemManFileLine'
C:\Define_New_problem\main.cpp:18: error: expected ';' before 'dPushMemManFileLine'
C:\Define_New_problem\main.cpp:21: error: expected ';' before '}' token
Process terminated with status 1 (0 minutes, 0 seconds)
Using MinGW。 (简化的)代码如下:
#include <iostream>
#define new dPushMemManFileLine( __FILE__, __LINE__ ) ? 0 : new
using namespace std;
static const int NUM_NEW_STACK_SIZE = 256;
static struct { const char* filename; unsigned int line; } g_NewStackMemDebug[NUM_NEW_STACK_SIZE];
static int g_CurStack = -1;
template <class T>
class mypair {
private:
int a, b;
public:
mypair (int first, int second)
{a=first; b=second;}
static void* operator new(size_t size)
{
}
};
int dPushMemManFileLine( const char* filename, unsigned int line )
{
if(g_CurStack >= NUM_NEW_STACK_SIZE )
return 0;
g_CurStack++;
g_NewStackMemDebug[g_CurStack].filename = filename;
g_NewStackMemDebug[g_CurStack].line = line;
return 0; // needed for the new passthrough trick
}
int main()
{
cout << "Hello world!" << endl;
return 0;
}
有人知道如何解决这个问题吗?
I'm trying to solve a part of my program, which has a #define new. Everything works well, until I try to create a class template that overrides the new operator, when I get the errors:
C:\Define_New_problem\main.cpp:18: error: expected type-specifier before 'dPushMemManFileLine'
C:\Define_New_problem\main.cpp:18: error: expected ';' before 'dPushMemManFileLine'
C:\Define_New_problem\main.cpp:21: error: expected ';' before '}' token
Process terminated with status 1 (0 minutes, 0 seconds)
Using MinGW. The (simplified) code is as follows:
#include <iostream>
#define new dPushMemManFileLine( __FILE__, __LINE__ ) ? 0 : new
using namespace std;
static const int NUM_NEW_STACK_SIZE = 256;
static struct { const char* filename; unsigned int line; } g_NewStackMemDebug[NUM_NEW_STACK_SIZE];
static int g_CurStack = -1;
template <class T>
class mypair {
private:
int a, b;
public:
mypair (int first, int second)
{a=first; b=second;}
static void* operator new(size_t size)
{
}
};
int dPushMemManFileLine( const char* filename, unsigned int line )
{
if(g_CurStack >= NUM_NEW_STACK_SIZE )
return 0;
g_CurStack++;
g_NewStackMemDebug[g_CurStack].filename = filename;
g_NewStackMemDebug[g_CurStack].line = line;
return 0; // needed for the new passthrough trick
}
int main()
{
cout << "Hello world!" << endl;
return 0;
}
Has anyone a good idea on how do I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
永远不要。
#define
.Keywords。拜托,只是不要。它只会造成问题。
#define
将所有出现的已定义单词替换为您定义的单词,因此在#define
的new
展开后,运算符重载如下所示:Uuups....
当然,问题仍然存在,你为什么这么做?
Don't.Ever.
#define
.Keywords.Please, just don't. It only makes problem. A
#define
replaces all occurences of the defined word with what you define it to be, so after the#define
'dnew
gets expanded, the operator overload looks like the following:Uuups....
Then of course the question remains, why did you do that?
我认为当您在
static void* operator new(size_t size) 行中展开
错误变得非常明显:new
时在关键字上使用
#define
是一个危险的游戏:)I think when you expand the
new
in the linestatic void* operator new(size_t size)
the error becomes pretty obvious:Using
#define
on keywords is a dangerous game :)正如前面提到的,出现错误是因为您的
#define new
也在operator new
处进行了扩展。为了解决您的问题,我建议您尝试以下操作;这将帮助您避免使用宏:
用法:
您唯一需要做的就是纠正对
new
的调用,直到它不再抛出
。您也可以通过简单的技术快速完成。 现在只需将每次对
new
的调用替换为NEW
即可。不要替换operator new
实现。然后执行,并编译代码。请注意,此宏的用法是为了调试目的并使事情变得更快。我不建议在生产代码中使用它。
如果您不想使用
NEW
等宏,也可以使用模板
来实现此目的。用法:
As it's mentioned, the error is coming because your
#define new
is expanded at theoperator new
also.To solve your problem, I can suggest you to try following; that will help you avoid using macros:
Usage:
Only effort you have to make is go and correct the call to
new
until it doesn'tthrow
. That also you can do it fast with a simple technique. Just doNow go and replace each call to
new
withNEW
. Don't replaceoperator new
implementation. Then do,And compile the code. Note that this macro usage is for your debugging purpose and make things fast. I am not recommending it to use in production code.
You can achieve this using
templates
also if you don't want to use macros likeNEW
.Usage: