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?
Don’t.Ever.
#define.Keywords.Please, just don’t. It only makes problem. A
#definereplaces all occurences of the defined word with what you define it to be, so after the#define‘dnewgets expanded, the operator overload looks like the following:Uuups….
Then of course the question remains, why did you do that?