C++头文件中的const数组声明

14 c++ const header

我有一个名为AppSettings的类,其中我有一个具有一系列音符频率的数组.我在下面的代码中遇到了几个错误,我不确定问题是什么.

错误消息是:

  • static data member of type 'const float [36] must be initialized out of line
  • A brace enclosed initializer is not allowed here before '{' token
  • Invalid in-class initialization of static data member of non-integral type

和代码:

class AppSettings{

public:
    static const float noteFrequency[36] = {
    //  C       C#      D       D#      E       F       F#      G       G#      A       A#      B
        130.81, 138.59, 146.83, 155.56, 164.81, 174.61, 185.00, 196.00, 207.65, 220.00, 223.08, 246.94,
        261.63, 277.18, 293.66, 311.13, 329.63, 349.23, 369.99, 392.00, 415.30, 440.00, 466.16, 493.88,
        523.25, 554.37, 587.33, 622.25, 659.25, 698.46, 739.99, 783.99, 830.61, 880.00, 932.33, 987.77
    };

};
Run Code Online (Sandbox Code Playgroud)

顾名思义,这只是一个头文件,包含我在整个应用程序中需要的一些设置和值.

KRy*_*yan 22

您无法在static类中定义类成员的值.你需要在课堂上有这样的一行:

class AppSettings
{
public:
    static const float noteFrequency[];
Run Code Online (Sandbox Code Playgroud)

然后在该类的实现文件中(AppSettings.cpp可能):

const float AppSettings::noteFrequency[] = { /* ... */ };
Run Code Online (Sandbox Code Playgroud)

此外,您无需在[]此处指定数字,因为C++足够智能,可以计算初始化值中的元素数量.

  • @ Cheersandhth.-Alf这与手头的问题毫无关系.这个答案写得很简洁.不将实现放在头文件中只是该语言的常识.我可以在SO上标记一半的解决方案,因为人们会把事情简明扼要,这不会让它错.如果您提供**额外**内容,它可能会使您的答案比这个更好. (4认同)

rub*_*nvb 11

这在C++ 11中运行得很好

class AppSettings{

public:
    static constexpr float noteFrequency[36] = {
//  C       C#      D       D#      E       F       F#      G       G#      A       A#      B
    130.81, 138.59, 146.83, 155.56, 164.81, 174.61, 185.00, 196.00, 207.65, 220.00, 223.08, 246.94,
    261.63, 277.18, 293.66, 311.13, 329.63, 349.23, 369.99, 392.00, 415.30, 440.00, 466.16, 493.88,
    523.25, 554.37, 587.33, 622.25, 659.25, 698.46, 739.99, 783.99, 830.61, 880.00, 932.33, 987.77
    };

};
Run Code Online (Sandbox Code Playgroud)


Che*_*Alf 7

C++ 03不支持复杂数据的类内定义,如常量数组.

要在头文件中将此类定义放在命名空间作用域中,并避免违反单一定义规则,您可以对模板类使用特殊豁免,如下所示:

#include <iostream>
using namespace std;

//----------------------------------------- BEGIN header file region
template< class Dummy >
struct Frequencies_
{
    static const double noteFrequency[36];
};

template< class Dummy >
double const Frequencies_<Dummy>::noteFrequency[36] =
{
    //  C       C#      D       D#      E       F       F#      G       G#      A       A#      B
    130.81, 138.59, 146.83, 155.56, 164.81, 174.61, 185.00, 196.00, 207.65, 220.00, 223.08, 246.94,
    261.63, 277.18, 293.66, 311.13, 329.63, 349.23, 369.99, 392.00, 415.30, 440.00, 466.16, 493.88,
    523.25, 554.37, 587.33, 622.25, 659.25, 698.46, 739.99, 783.99, 830.61, 880.00, 932.33, 987.77
};

class AppSettings
    : public Frequencies_<void>
{
public:
};
//----------------------------------------- END header file region

int main()
{
    double const a = AppSettings::noteFrequency[21];

    wcout << a << endl;
}
Run Code Online (Sandbox Code Playgroud)

还有一些其他技术可以使用:

  • 内联函数,用于生成对数组的引用(或用作索引器).

  • 将定义放在单独编译的文件中.

  • 只需根据需要计算数字.

没有更多的信息,我不想为你做出选择,但这不应该是一个艰难的选择.