Sign in to follow this  
becklighter

Class Static Array Question

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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:

[code]
// 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
[/code]

See also "Fixing Problem 4" in [url=http://www.gamedev.net/page/resources/_/reference/programming/languages/c-and-c/organizing-code-files-in-c-and-c-r1798]Organizing Code Files in C and C++[/url].

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='A Brain in a Vat' timestamp='1313359094' post='4849130']
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.
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this