How can I create const arrays and const computed values in a C ++ class?
I am facing some compiler errors that I do not understand. I'm pretty sure I'm doing something really bad here, but I don't know what. I would like all world constants to be defined as belonging to a class.
Notes:
I only use classes as structs with attached members. I am not strictly OO design. Please do not comment out public variables.
I'm not really worried about the compiler the information is embedded in. I use this framework because it is easy for me to use. (If it worked)
class Board{
public:
enum PhysicsResult{ BOUNCE, OUT_OF_BOUNDS_TOP, OUT_OF_BOUNDS_BOTTOM, CONTINUE };
//World constants
const static float Height = 500;
const static float Width = 300;
//ERROR: 'Board::Width' cannot appear in a constant-expression.
const static float PaddleWidth = Width/15;
const static float BallRadius = 5;
const static float BounceDistance = 1.5;
//World Objects
Ball ball;
Paddle paddle1;
Paddle paddle2;
/*
1---2
| |
0---3
*/
//ERROR: a brace-enclosed initalizer is not allowed here before '{' token
//ERROR: invalid in-class initalization of static data member of nonintegral type 'const Pair[4]'
const static Pair corners[4] = {Pair(0, 0), Pair(0, Height), Pair(Width, Height), Pair(Width, 0)};
//ERROR: a brace-enclosed initalizer is not allowed here before '{' token
//ERROR: invalid in-class initalization of static data member of nonintegral type 'const Pair[2]'
const static Pair left_wall[2] = {corners[0], corners[1]};
//ERROR: a brace-enclosed initalizer is not allowed here before '{' token
//ERROR: invalid in-class initalization of static data member of nonintegral type 'const Pair[2]'
const static Pair right_wall[2] = {corners[3], corners[2]};
//ERROR: a brace-enclosed initalizer is not allowed here before '{' token
//ERROR: invalid in-class initalization of static data member of nonintegral type 'const Pair[2]'
const static Pair top_wall[2] = {corners[1], corners[2]};
//ERROR: a brace-enclosed initalizer is not allowed here before '{' token
//ERROR: invalid in-class initalization of static data member of nonintegral type 'const Pair[2]'
const static Pair bottom_wall[2] = {corners[0], corners[3]};
If possible, what is the correct syntax for this? If this is not possible, what alternative should I use?
a source to share
Defining static constants outside of the class body is compiled and executed with gcc.
#include <iostream>
using namespace std;
struct Pair { int a; int b; Pair(int x, int y) : a(x),b(y) {}};
struct F {
static const float blah = 200.0;
static const Pair corners[4];
};
// square boards are so ordinary
const Pair F::corners[4] = { Pair(0,0), Pair(0,1), Pair(2,0), Pair(2,2) };
const float F::blah ;
int main(int, char **) {
cout << F::corners[0].a << endl ;
cout << F::blah << endl;
return 0;
}
I cannot overstate the importance of ebo's comment about initialization order.
a source to share
The static members of C ++ objects must be defined outside of the declaration. This is because the compiler does not know which translation unit (.o file) to put the members in.
I usually define them in the .cpp implementation file. Usually you don't want to put them in a header file because they will end up in multiple .o files and it will generate compiler errors because the same value is defined multiple times.
a source to share