c++ - IntelliSense:类型为 "const char *const []"的成员不能具有类内初始化程序

标签 c++ arrays visual-studio-2013 intellisense const-char

<分区>

考虑:

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

虽然编译正常, IntelliSense 给我一个错误:

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

这是错误还是我做错了什么?

最佳答案

根据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" };

关于c++ - IntelliSense:类型为 "const char *const []"的成员不能具有类内初始化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24278473/

相关文章:

c++ - 如何在不设置值的情况下通知 C++ future?

Linux 的 C++ 调试器类似于 MS Visual Studio 的调试器?

visual-studio-2012 - 如何更改 Visual Studio 2012、2013 或 2015 许可证 key ?

c++ - QLcdNumber 奇怪的行为

c++ - 将 MFC 库移植到 linux 和 ASSERT

javascript - 测试一个字符串数组的indexOf 与另一个数组?

javascript - jQuery 使用 .each( ) 循环遍历 .hover( ) 函数

java - 来自 2 个(或更多)字符串的压缩字符串 - "AB"+ "YZ"= "AYBZ"

c++ - 预编译头文件中的错误 C2512?

c++ - 处理数百万个排列