AIX unix服务器上的g ++编译错误

时间:2015-05-15 08:16:23

标签: c++ g++ aix

我有一个用C ++编写的套接字程序服务器代码。

使用g ++编译器(OS:Unix AIX)编译时,我遇到以下错误。使用cc编译器(OS:Unix Sun OS)成功编译了相同的代码。请让我知道如何解决它。

  • 代码段

    enum sockStates
    
            {
    
                    inopen  = ios::in,
    
                    outopen = ios::out,
    
                    open    = ios::in | ios::out,
    
            };
    
  • 错误

    g++ -gxcoff -maix64 -shared -fpic -fpermissive -w -libstd=c++11ox -I/devt/flex/java/include -I/devt/flex/java/include/aix -I/tmp/ribscomp/server/include -c -o server.o server.cc
    
     ssocket.h:721:26: error: calls to overloaded operators cannot appear in a constant-expression
        open = ios::in | ios::out,
                             ^ 
    
  • g ++版

    g++ -v
    

使用内置规格。

COLLECT_GCC=g++

COLLECT_LTO_WRAPPER=/opt/freeware/libexec/gcc/powerpc-ibm-aix7.1.0.0/4.8.2/lto-wrapper

Target: powerpc-ibm-aix7.1.0.0

Configured with: ../gcc-4.8.2/configure --with-as=/usr/bin/as --with-ld=/usr/bin/ld --enable-languages=c,c++,fortran --prefix=/opt/freeware --mandir=/opt/freeware/man --infodir=/opt/freeware/info --enable-version-specific-runtime-libs --disable-nls --enable-decimal-float=dpd --host=powerpc-ibm-aix7.1.0.0

Thread model: aix

gcc version 4.8.2 (GCC)

2 个答案:

答案 0 :(得分:3)

问题是GCC的标准库将std::ios::openmode定义为具有重载运算符的枚举类型,而在C ++ 03中,不允许这些运算符出现在常量表达式中(例如初始化程序)对于普查员而言。)

它适用于Solaris编译器,因为(我假设)openmode只是一个整数类型。标准允许,因此两个编译器都符合。

在C ++ 11模式下,运算符为constexpr并且可以在此处使用,因此一种解决方案是使用-std=c++11-std=gnu++11进行编译,但要注意C的ABI ++ 11类型未在GCC 4.8中最终确定。

另一个解决方案是用常量变量替换枚举数:

enum sockstates { _max = std::numeric_limits<int>::max() };

const sockstates inopen  = ios::in;
const sockstates outopen = ios::out;
const sockstates open    = ios::in | ios::out;

答案 1 :(得分:1)

无论是否有错误,都不要这样做。这些是std::ios_base::openmode类型的枚举器,您希望使用此类型,而不是您自己的枚举类型。如果您确实想要重命名/别名标准库实体:

namespace sockStates
{
  static const ios::openmode
      inopen  = ios::in,
      outopen = ios::out,
      open    = ios::in | ios::out;
};

我不建议这样做。只需在任何地方明确使用ios::in | ios::out即可。一个普通的C ++程序员立即知道这意味着什么。