2014-06-18 53 views
0

考慮:智能感知:類型的成員 「爲const char * const的[]」 不能有一流初始化

class Note 
{ 
public: 
    // ... 
private: 
    static const char* const NOTE_NAMES[12] = 
      { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" }; 
} 

雖然它編譯罰款, 智能感知給了我一個錯誤:

IntelliSense: a member of type "const char *const [12]" cannot have an in-class initializer

這是一個錯誤還是我做錯了什麼?

+0

您需要添加'constexpr'此代碼是有效的有效代碼。 –

+0

C++標準只允許在類內初始化靜態常量積分或枚舉類型。 – 101010

+0

對不起,重複。 – Xan

回答

1

按照C++標準

If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignmentexpression is a constant expression

所以,你的代碼不符合C++標準。

在另一方面

A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression

因此,要獲得你應該寫

static constexpr char* const NOTE_NAMES[12] = 
     { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" }; 
相關問題