爲什麼以下C++ 11/14代碼不起作用?在這裏,我正在向班內宣佈一個枚舉。目標不是在課堂上有100多個枚舉值 - 這使得課程無法讀取。出於政治原因,我無法爲此使用單獨的範圍枚舉。在課堂上抱怨私信
class A {
public:
// forward declare the enum
enum B : int;
void func (B b) {}
};
// The actual declaration
enum A::B : int {
Val1,
Val2,
};
int main()
{
// B is unscoped. so I should be able to use it as below?
int a = A::Val1;
}
編譯錯誤
tmp.cpp: In function ‘int main()’:
tmp.cpp:13:5: error: ‘A::B Val1’ is private
Val1,
^
tmp.cpp:19:16: error: within this context
int a = A::Val1;
^
但是,下面的代碼工作:將出現在G中
int a = A::B::Val1;
只是一個想法:也許是因爲編譯器無法知道'Val1'不超過一個枚舉在A'的'的範圍內聲明的或派生階級 - 並通過強制它成爲私人來防止這種可能性。 – Jeremy