Declaring a const array in a C ++ header file

I have a class called AppSettings where I have an array with note frequency range. I am getting a few errors with the code below and I'm not sure what the problem is.

Error messages:

  • 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] = { // CC# DD# EFF# GG# AA# 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 implies, this is just a header file with some settings and values ​​that I need throughout the application.

+7
source share
3 answers

You cannot determine the value of static class members in a class. You should have a line like this in the class:

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

And then in the implementation file for the class ( AppSettings.cpp possible):

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

Also, there is no need to specify the number inside [] , because C ++ is smart enough to count the number of elements in your initialization value.

+16
source

This works fine in C ++ 11

 class AppSettings{ public: static constexpr float noteFrequency[36] = { // CC# DD# EFF# GG# AA# 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 }; }; 
+10
source

C ++ 03 does not support class definitions of complex data, such as constant arrays.

To place such a definition in the namespace area in the header file and not violate the definition rule, you can use the special exception 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] = { // CC# DD# EFF# GG# AA# 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 other methods that you can use:

  • A built-in function that creates an array reference (or used as an indexer).

  • Placement of the definition in a separately compiled file.

  • Simple calculation of numbers as needed.

Without additional information, I would not want to make a choice for you, but this should not be a difficult choice.

+8
source

All Articles