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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11693690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:25:17  来源:igfitidea点击:

const array declaration in C++ header file

c++headerconst

提问by KRyan

I have a class called AppSettings where I have an Array with a range of note frequencies. I'm getting several errors with the code below and I'm not sure what the problem is.

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

The error messages are:

错误消息是:

  • 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
  • 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

And the code:

和代码:

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
    };

};

As the name suggests this is just a header file with some settings and values I need throughout the app.

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

回答by KRyan

You can't define the value of staticclass members within the class. You need to have a line like this in the class:

您不能在static类中定义类成员的值。你需要在课堂上有这样一行:

class AppSettings
{
public:
    static const float noteFrequency[];

And then in an implementation file for the class (AppSettings.cppperhaps):

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

const float AppSettings::noteFrequency[] = { /* ... */ };

Also, you don't need to specify the number within the []here, because C++ is smart enough to count the number of elements in your initialization value.

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

回答by rubenvb

This works just fine in C++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
    };

};

回答by Cheers and hth. - Alf

C++03 doesn't support in-class definitions of complex data like arrays of constants.

C++03 不支持复杂数据(如常量数组)的类内定义。

To place such a definition at namespace scope in a header file, and avoid breaking the One Definition Rule, you can leverage a special exemption for template classes, as follows:

要将这样的定义放在头文件中的命名空间范围内,并避免破坏单一定义规则,您可以利用模板类的特殊豁免,如下所示:

#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;
}

There are also some other techniques that can be used:

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

  • An inline function producing a reference to the array (or used as indexer).

  • Placing the definition in a separately compiled file.

  • Simply computing the numbers as needed.

  • 内联函数产生对数组的引用(或用作索引器)。

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

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

Without more information I wouldn’t want to make the choice for you, but it shouldn’t be a difficult choice.

如果没有更多信息,我不想为您做出选择,但这应该不是一个困难的选择。