宏定义后面不要加分号-会产生诡异的编译错误

写程序时间长了,没句末尾都习惯性的的加上一个分号,对于宏定义来说,万万不可!

看一个例子

代码
#include <iostream>
#include
<iomanip>
using namespace std ;

// Do not place the ';' after Macro definition!!!
// You will got very strange error
#define PI 3.14; // the semicolon here is forbidden!!!

double Area(double pi, double r)
{
return pi * r * r ;
}

int main(void)
{
cout
<< Area(PI, 2.0f) << endl ;

getchar() ;
}

 

我在VS2008下面编译,得到如下错误信息

Error 1 error C2143: syntax error : missing ')' before ';' d:\codes\directx\test\test2\main.cpp 16
Error 2 error C2660: 'Area' : function does not take 1 arguments d:\codes\directx\test\test2\main.cpp 16
Error 3 error C2143: syntax error : missing ';' before ',' d:\codes\directx\test\test2\main.cpp 16
Error 4 error C2059: syntax error : ')' d:\codes\directx\test\test2\main.cpp 16

可见这个多余的分号是多么可恶!

另外,宏定义中最好也不要加空格,而且对于多个参数或者表达式来说,最好用括号括上。能用常量代替的地方就不要用宏了。。。

posted on 2010-11-19 15:31  前端风云志  阅读(1124)  评论(0)    收藏  举报

导航

关注我

前端风云志