简体   繁体   English

具有范围解析度和条件的C ++三元运算符

[英]C++ Ternary operator with scope resolution and condition

Following code is not being compiled by an specific compiler. 以下代码未由特定的编译器编译。

#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;
}

Compiler g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11) compiled it successfully. 编译器g ++(GCC)4.8.5 20150623(Red Hat 4.8.5-11)已成功编译。

Compiler g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17) is causing compilation error 编译器G ++(GCC)4.4.7 20120313(Red Hat 4.4.7-17)导致编译错误

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

If I replace condition (a==b) in line int z = (a==b)?(A::x):(A::y); 如果我在int z = (a==b)?(A::x):(A::y);行中替换条件(a==b) int z = (a==b)?(A::x):(A::y); by true or false , then it gets compiled successfully. truefalse ,则可以成功编译。

What is the reason and how to fix it for specified compiler? 原因是什么,如何针对指定的编译器进行修复?

Looks like a weak/buggy C++0x symbol-linkage implementation of gcc 4.4. 看起来像gcc 4.4的弱/笨拙的C ++ 0x符号链接实现。 Seems that gcc 4.4 tells the linker that there are symbols but it forgot to "implement" them in one of the compilation units. 似乎gcc 4.4告诉链接器有符号,但它忘记了在一个编译单元中“实现”它们。

I guess if you put the initialization of the static members A::x and A::y explicitly into one unique compilation unit (eg corresponding .cpp file) then your code may be compatible with both compilers. 我想如果您将静态成员A :: x和A :: y的初始化显式地放入一个唯一的编译单元(例如,相应的.cpp文件)中,那么您的代码可能与这两个编译器都兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM