C 三元操作员具有范围分辨率和条件

C++ Ternary operator with scope resolution and condition

本文关键字:范围 分辨率 条件 操作员 三元      更新时间:2023-10-16

以下代码未由特定编译器编译。

#include <iostream>
using namespace std;
class A{
        public:
                static const int x = 12;
                static const int y = 16;
};
int main(){
        int a = 12, b = 19;
        int z = (a==b)?(A::x):(A::y);
        cout<<z<<endl;
        return 0;
}

编译器 G (GCC(4.8.5 20150623(Red Hat 4.8.5-11(成功地编译了。

编译器 G (GCC(4.4.7 20120313(红色帽子4.4.7-17(正在引起编译错误

test.cpp:(.text+0x20): undefined reference to `A::x'
test.cpp:(.text+0x28): undefined reference to `A::y'

如果我用truefalse替换了int z = (a==b)?(A::x):(A::y);中的条件(a==b),则将其成功编译。

如何解决指定编译器的原因以及如何修复?

看起来像弱/buggy C 0x符号 - 链接实现GCC 4.4。GCC 4.4似乎告诉链接器有符号,但忘了在其中一个编译单元中"实现"它们。

我猜想,如果将静态成员的初始化a :: x和a :: y明确地放入一个唯一的编译单元(例如,相应的.cpp文件(,则您的代码可能与两个编译器兼容。