2012-08-25 31 views
3

我在我的舊代碼這個如何在遺留代碼處理方法名重複

#define max(x, y) (x > y ? x : y) 
#define min(x, y) (x < y ? x : y) 

這個bean使用配發的應用程序,現在我嘗試編譯它在FreeBSD 和我不斷收到:

/usr/include/c++/4.2/bits/istream.tcc:123:35: error: macro "min" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:124:45: error: macro "max" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:143:33: error: macro "min" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:144:43: error: macro "max" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:438:48: error: macro "max" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:441:53: error: macro "min" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:449:47: error: macro "max" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:489:48: error: macro "max" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:493:53: error: macro "min" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:501:47: error: macro "max" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:507:53: error: macro "max" requires 2 arguments, but only 1 given 
/usr/include/c++/4.2/bits/istream.tcc:806:43: error: macro "max" requires 2 arguments, but only 1 given 

我猜它的代碼(宏)中的方法的名稱。
現在改名爲它的工作。
我怎樣才能繼續使用它,但避免編譯器混淆?

+0

你怎麼調用宏? –

+0

如何首先包含系統包含文件,#undefining min和max以及最後包含您的遺留包含文件? –

回答

4

首先定義這些宏的原因是什麼?它是C++,不需要任何宏,尤其是那些已經被標準給予你的函數。(包括<windows.h>和得到他們愚蠢的minmax宏的投訴總是困擾我)。

話雖這麼說,一個快速和骯髒的解決方案可能與

#include <algorithm> 
using std::min; 
using std::max; 

雖然代替你的宏定義,這污染了全局命名空間中那些現在可以被任何局部變量隱藏正確的函數名或任何其他功能或方法,並不只是被一個愚蠢的文本替換預處理器所替代。

除此之外考慮在這些宏之前包含任何系統包含文件(或using s)。