Monday, November 06, 2006

stl fstream and macros on vs7

while working with visual studio 2003 / 7, c++, fstream, log files and macros, like the following:

#define LOGMACRO (format_string, ...) log (__FILE__, __LINE__, format_string, __VA_ARGS__)


got some errors:
error C2059: syntax error : '...'
error C2061: syntax error: identifier '_DebugHeapTag'
xdebug(29): _CRTIMP2 void * __cdecl operator new(size_t, const std::_DebugHeapTag_t&, char *, int)
_THROW1(std::bad_alloc); // allocate from the debug CRT heap


the solution was in 4 steps:

1. replacing all stl include files matching the pattern:
#include <string.h>
#include <fstream.h>

with header files without '.h' in their names, ex:
#include <string>
#include <fstream>


2. putting some parantheses around the parameters in the macro def. value, like:
#define LOGMACRO (format_string, ...) log (__FILE__, __LINE__, (format_string), __VA_ARGS__)


3. replacing '...' and '__VA_ARGS__' with a simple parameter. vs7 does not support varargs in macros.
#define LOGMACRO (format_string, args) log (__FILE__, __LINE__, (format_string), (args))


4. putting the fstream include in the last line of stdafx.h, removing from any other location in the source code like below.
#include <fstream>
using namespace std;


In addition to the things described above, here is a nice macro tutorial: http://en.wikipedia.org/wiki/C_preprocessor

No comments: