[英]C++ Ternary operator with scope resolution and condition
以下代碼未由特定的編譯器編譯。
#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(Red Hat 4.4.7-17)導致編譯錯誤
test.cpp:(.text+0x20): undefined reference to `A::x'
test.cpp:(.text+0x28): undefined reference to `A::y'
如果我在int z = (a==b)?(A::x):(A::y);
行中替換條件(a==b)
int z = (a==b)?(A::x):(A::y);
按true
或false
,則可以成功編譯。
原因是什么,如何針對指定的編譯器進行修復?
看起來像gcc 4.4的弱/笨拙的C ++ 0x符號鏈接實現。 似乎gcc 4.4告訴鏈接器有符號,但它忘記了在一個編譯單元中“實現”它們。
我想如果您將靜態成員A :: x和A :: y的初始化顯式地放入一個唯一的編譯單元(例如,相應的.cpp文件)中,那么您的代碼可能與這兩個編譯器都兼容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.