2016-08-19 68 views
0

所以我被困在一個討厭的問題,並花了一個小時已經在調查。C++錯誤:'內聯'只能出現在功能上

我有一個相當老的C++ - ish代碼的項目,我需要向它添加一些C++ 11代碼。該項目之前編譯,所以這是相當肯定的是,引入該問題由我添加以下到我的CMakelist.txt:

set (CMAKE_CXX_STANDARD 11) 

的問題是由這些定義造成的:

#define lt(a, b) ( ((a) < -b)) 
#define ge(a, b) (! ((a) < -b)) 
#define le(a, b) ( ((a) <= b)) 
#define gt(a, b) (! ((a) <= b)) 
#define eq(a, eps) ((((a) <= eps) && !((a) < -eps))) 
#define ne(a, eps) (!eq(a,eps)) 

這是我得到的錯誤:

/Users/bs/util.h:284:22: note: expanded from macro 'eq' 
#define eq(a, eps) ((((a) <= eps) && !((a) < -eps))) 
        ^
In file included from /Users/bs/geom.cc:35: 
In file included from /Users/bs/coord.h:30: 
In file included from /Users/bs/vronivector.h:6: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:678:12: error: 'inline' can 
     only appear on functions 
    static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT 
     ^
fatal error: too many errors emitted, stopping now [-ferror-limit=] 
20 errors generated. 
make[2]: *** [src/CMakeFiles/vroni.dir/geom.cc.o] Error 1 
make[1]: *** [src/CMakeFiles/vroni.dir/all] Error 2 
make: *** [all] Error 2 

這裏有什麼問題,它可以修復嗎?

+0

['std :: char_traits :: eq'](http: //en.cppreference.com/w/cpp/string/char_traits/cmp),宏一直是錯的 – MSalters

+1

出於興趣你爲什麼要定義這些宏,以及爲什麼宏? –

+0

正如我所說的,我必須處理遺留代碼。簡而言之:非常複雜的代碼,但是從數學的角度來看,代碼已經過很好的測試,從軟件角度來看,代碼非常笨爲了讓遺留代碼正常工作,我實際上重新整理了整個項目 - 重構的嘗試造成了太多的頭痛。 – benjist

回答

2

您對符號'eq'有一個名稱衝突不幸的是,c樣式宏執行文字替換,因此與eq相關的文本被填充到您看到錯誤消息的語句中,該語句產生如下所示的錯誤消息:

static inline _LIBCPP_CONSTEXPR bool ((((char_type __c1) <= eps) && !(char_type __c1) < -eps)) _NOEXCEPT 

最好的解決方案是停止使用宏並找到另一種方法來定義'eq''lt'等功能。

,你可以,例如用像更換EQ宏:

template<typename T>bool eq(T a,T eps) 
{ 
    return (a <= eps && a >= -eps); 
} 

一個不那麼好的解決辦法是拿出你的宏的命名約定是不太可能的符號衝突這將在其他包中使用(例如,您可以命名您的宏EQ或EQ_(不要使用前導下劃線,這是一個整體的問題)。