C ++错误:'内联'只能出现在函数

时间:2016-08-19 20:29:02

标签: 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

这里的问题是什么,可以修复吗?

1 个答案:

答案 0 :(得分:2)

你的符号'eq'有一个名称冲突不幸的是,c样式的宏做了一个文字文本替换,所以与eq相关的文本被填充到你看到错误信息的语句中,产生的结果如下: / p>

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_(不要使用领先的下划线,这是一个完整的问题。)