添加 #define new 时不是可识别的运算符或类型

发布于 2024-11-06 06:04:42 字数 1364 浏览 0 评论 0原文

我正在尝试解决我的程序的一部分,其中有一个#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 技术交流群。

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

发布评论

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

评论(3

夏至、离别 2024-11-13 06:04:42

永远不要。#define.Keywords。

拜托,只是不要。它只会造成问题。 #define 将所有出现的已定义单词替换为您定义的单词,因此在 #definenew 展开后,运算符重载如下所示:

static void* operator dPushMemManFileLine( __FILE__, __LINE__ ) ? 0 : new(size_t size)
    {
    }

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'd new gets expanded, the operator overload looks like the following:

static void* operator dPushMemManFileLine( __FILE__, __LINE__ ) ? 0 : new(size_t size)
    {
    }

Uuups....
Then of course the question remains, why did you do that?

橘香 2024-11-13 06:04:42

我认为当您在 static void* operator new(size_t size) 行中展开 new
错误变得非常明显:

class mypair {
    static void* operator dPushMemManFileLine( __FILE__, __LINE__ ) ? 0 : new(size_t size)
    { }
};

在关键字上使用 #define 是一个危险的游戏:)

I think when you expand the new in the line static void* operator new(size_t size)
the error becomes pretty obvious:

class mypair {
    static void* operator dPushMemManFileLine( __FILE__, __LINE__ ) ? 0 : new(size_t size)
    { }
};

Using #define on keywords is a dangerous game :)

念﹏祤嫣 2024-11-13 06:04:42

正如前面提到的,出现错误是因为您的 #define new 也在 operator new 处进行了扩展。

为了解决您的问题,我建议您尝试以下操作;这将帮助您避免使用宏:

//overload new with LINE & FILE and call the function inside
void* operator new (size_t size, const char *FILE, const unsigned int LINE)
{
  dPushMemManFileLine(FILE, LINE);
  return malloc(size);
}

//overload the default new operator and throw something vague    
void* operator new (size_t size)
{ 
  class BadNewUsed {} bad;
  throw bad;  // throw it; since we can't catch it at compile time
}

用法:

int *pi = new(__LINE__, __FILE__) int; // ok
double *pd = new double; // throws BadNewUsed out of your code,

您唯一需要做的就是纠正对 new 的调用,直到它不再抛出。您也可以通过简单的技术快速完成。 现在只需

#define new abcd // to get compilation error wherever new is present

将每次对 new 的调用替换为 NEW 即可。不要替换 operator new 实现。然后执行,

#define NEW new( __FILE__, __LINE__)

并编译代码。请注意,此宏的用法是为了调试目的并使事情变得更快。我不建议在生产代码中使用它。

如果您不想使用 NEW 等宏,也可以使用模板 来实现此目的。

template<typename T>
T* NEW (const char* FILE, const unsigned int LINE)
{
  dPushMemManFileLine(FILE, LINE);
  return new T;  // can use default version or any overloaded version of 'new'
}

用法:

int *p = NEW<int>(__FILE__, __LINE__);

As it's mentioned, the error is coming because your #define new is expanded at the operator new also.

To solve your problem, I can suggest you to try following; that will help you avoid using macros:

//overload new with LINE & FILE and call the function inside
void* operator new (size_t size, const char *FILE, const unsigned int LINE)
{
  dPushMemManFileLine(FILE, LINE);
  return malloc(size);
}

//overload the default new operator and throw something vague    
void* operator new (size_t size)
{ 
  class BadNewUsed {} bad;
  throw bad;  // throw it; since we can't catch it at compile time
}

Usage:

int *pi = new(__LINE__, __FILE__) int; // ok
double *pd = new double; // throws BadNewUsed out of your code,

Only effort you have to make is go and correct the call to new until it doesn't throw. That also you can do it fast with a simple technique. Just do

#define new abcd // to get compilation error wherever new is present

Now go and replace each call to new with NEW. Don't replace operator new implementation. Then do,

#define NEW new( __FILE__, __LINE__)

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 like NEW.

template<typename T>
T* NEW (const char* FILE, const unsigned int LINE)
{
  dPushMemManFileLine(FILE, LINE);
  return new T;  // can use default version or any overloaded version of 'new'
}

Usage:

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