Jump to content
  • Advertisement
Sign in to follow this  
becklighter

Class Static Array Question

This topic is 2563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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++.

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

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!