开发者

Stange error with preprocessor macro [closed]

开发者_运维百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

i have changed code style and aslo i hope it will help http://www.cs.bell-labs.com/cm/cs/pearls/spacemod.cpp

#include <iostream>
using namespace std;

#define MEASURE(T,  text)
{        \
   int i;
cout<<text<<"\t";                       \

 cout<<sizeof(T)<<"\t";                     \
 int lastp=0;\
  for ( i=0;i<11;i++){                \
      T *p=new T;                                  \
      int thisp=(int)p;                                   \
       if (lastp!=0)   cout<<" "<<thisp-lastp;           \
       lastp=thisp;                             \
  }                                                                     \
  cout<<"n";\
  }                                                     \
  using namespace std;
template <class T>
void measure(char *text)
{
    cout<<"measure"<<text<<"\t";  
    cout<<sizeof(T)<<"\n";
}
int main(){



     return 0;
}

it has only one mistake

1>c:\users\david\documents\visual studio 2010\projects\measure\measure.cpp(5): error C2447: '{' : missing function header (old-style formal list?)


I'm pretty certain you need a \ at the end of the #define line itself as well as the int i;, and possibly the empty lines in that macro as well (I'm not sure about that).

I suspect the \ on the line preceding your using statement is dodgy as well.

However, the one thing I'm absolutely certain about is that you would be better off using inline functions than #define macros.

While inline doesn't guarantee that it's actually inlined, I tend to always leave optimisation up to the compiler.

And code macros, I frown on that pretty heavily in C++, especially for complex things like this :-)


You are missing a line continuation character on the first line of the macro. You can see what your macros expand into by running the preprocessor (e.g with G++ use the -E flag).

That said, I don't see why you should need a macro in the first place. Why not use a simple template function?


Ok, its the continuation character after the #define as you got from the prevoius answears. You should try to avoid defines as far as possible, exactly because they can lead to such errors which don't mean anything. I think you did that define to increase performance, but there is a better way to do this. Define this function as an "inline" function in the header. You can read more about inline functions for example here: http://www.cplusplus.com/doc/tutorial/functions2/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜