Class Static Array Question

Started by
3 comments, last by becklighter 12 years, 8 months ago
I'm attempting to define a static multi-dimensional array in a class, but keep getting the very generic error message "syntax error : '{'" on the line with the opening "{".
Any suggestions on what I might be doing wrong?

Here is an example of the code:

[source]
class GameBoard{
private:
static const int numberOfPieces = 1;
static const int numberOfRotations = 4;
static const int pieceRows = 4;
static const int pieceColumns = 4;
static const int gamePiece[numberOfPieces][numberOfRotations][pieceRows][pieceColumns] = {
{// ::
{
{ 0, 0, 0, 0},
{ 0,-1,-1, 0},
{ 0,-1,-1, 0},
{ 0, 0, 0, 0}
},
{
{ 0, 0, 0, 0},
{ 0,-1,-1, 0},
{ 0,-1,-1, 0},
{ 0, 0, 0, 0}
},
{
{ 0, 0, 0, 0},
{ 0,-1,-1, 0},
{ 0,-1,-1, 0},
{ 0, 0, 0, 0}
},
{
{ 0, 0, 0, 0},
{ 0,-1,-1, 0},
{ 0,-1,-1, 0},
{ 0, 0, 0, 0}
}
}
};
};
[/source]
Advertisement
Unlike Java or C#, static members and global variables (those qualified with "extern") in C++ have external linkage, meaning that to other compilation units that include the header file containing those static members or global variables, must hope that the linker can provide them with a definition:


// File: "foobar.h"
#pragma once

class foobar {
private:
// there exists in this program only one foobar::counter_, only one foobar::array_, ...
static int counter_;
static int array_[8][3][4];
};

// ... and only one global_state
extern int global_state;

// File: "foobar.cpp"
#include "foobar.h"

// the foobar.cpp's object code will provide the linker with the definitions
int foobar::counter_ = 0;
int foobar::array_[8][3][4] = {};
int global_state = 0;

// the linker will link the foobar::counter_ and others from other object code to these definitions


See also "Fixing Problem 4" in Organizing Code Files in C and C++.
Thank you for the quick reply. Most of my experience is with other languages, but I'm trying to learn C++ better.
This has nothing to do with external linkage.

becklighter, you can only initialize class member variables like that if they are static const and of an integral (including enumerations) type.

Why only integral types? Because that's what the standard says. You need to provide a definition for an object before you can initialize it, and in the .h file you haven't yet provided a definition, only a declaration. So you need to provide its definition in a cpp file, as fastcall22 showed.

So why can integral types provide const initializers but no other types can? Because the creators of C++ recognized that there should be a way to do this, but only came up with a good way to do with with integers. It's one of the uglier aspects of C++, and has been fixed to a degree in C++0x.

This has nothing to do with external linkage.

becklighter, you can only initialize class member variables like that if they are static const and of an integral (including enumerations) type.

Why only integral types? Because that's what the standard says. You need to provide a definition for an object before you can initialize it, and in the .h file you haven't yet provided a definition, only a declaration. So you need to provide its definition in a cpp file, as fastcall22 showed.

So why can integral types provide const initializers but no other types can? Because the creators of C++ recognized that there should be a way to do this, but only came up with a good way to do with with integers. It's one of the uglier aspects of C++, and has been fixed to a degree in C++0x.


Thank you for the explanation, I really like to understand why something works the way it does.

This topic is closed to new replies.

Advertisement