Problem Initializing array in devc++

Started by
4 comments, last by mathacka 11 years, 11 months ago
I'm trying to make a Tetris app and describe the number of states each block has.

Here's my code snippet with error included:




const int BLOCK_TYPE_NUM = 6; // number of total block types

int block_states[BLOCK_TYPE_NUM];

block_states[0] = 1; // expected constructor, destructor, or type conversion before '=' token
block_states[1] = 2; // expected constructor, destructor, or type conversion before '=' token
block_states[2] = 2; // expected constructor, destructor, or type conversion before '=' token
block_states[3] = 4; // expected constructor, destructor, or type conversion before '=' token
block_states[4] = 4; // expected constructor, destructor, or type conversion before '=' token
block_states[5] = 4; // expected constructor, destructor, or type conversion before '=' token


So, here's the error for each line containing "block_state" array:

16 ..\Tetris Apps\main.cpp expected constructor, destructor, or type conversion before '=' token

Thanks for taking a look at this guys,

mathacka
Advertisement

int block_states[BLOCK_TYPE_NUM] = {
1, 2, 2, 4, 4, 4
};


You cannot have code outside of functions. (When do they get executed? What about any state it might rely on?)

EDIT:
Looks like this is inside a class, in which case:

// Foobar.h
class Foobar {
private:
static int block_state[BLOCK_TYPE_NUM];
};

// Foobar.cpp
#include "Foobar.h"

int Foobar::block_state[BLOCK_TYPE_NUM] = {
1, 2, 2, 4, 4, 4
};


EDIT:
Or:

// Foobar.h
class Foobar {
public:
Foobar();

private:
int block_state[BLOCK_TYPE_NUM];
};

// Foobar.cpp
#include "Foobar.h"
#include <algorithm>

Foobar::Foobar() {
static const int arr[BLOCK_TYPE_NUM] = {
1, 2, 2, 4, 4, 4
};
std::copy( arr, arr + 6, block_state );
}
Since you mentioned devC++ I feel compelled to urge you to use something better, their last release was in 2005! Microsoft Visual C++ Express is much better and free.
UPDATE

Got block_states to work by:


int block_states[BLOCK_TYPE_NUM] = { 1,2,2,4,4,4 };


but now having a similar error as before with this similar code:


// block.h

// Type Initialization
const int STATES = 4; // number of states
const int BLOCK_COUNT = 4; // number of blocks
const int BLOCK_TYPE_NUM = 6; // number of total block types

int state = 0;
int block_type_num = 0;

int block_states[BLOCK_TYPE_NUM] = { 1,2,2,4,4,4 };

// User Defined Types
struct BLOCK_MESH
{ int x;
int y;
};

struct BLOCK_POS
{ int x;
int y;
};

BLOCK_MESH block[STATES][BLOCK_COUNT][BLOCK_TYPE_NUM];
BLOCK_POS[BLOCK_TYPE_NUM]; // expected unqualified-id before '[' token


Thanks, and here's the error out of code:

34 ..\Tetris Apps\block.h expected unqualified-id before '[' token

Later,

mathacka


BLOCK_POS[BLOCK_TYPE_NUM]; // expected unqualified-id before '[' token


You haven't given it a variable name, e.g.:

BLOCK_POS positions[BLOCK_TYPE_NUM];


int block_states[BLOCK_TYPE_NUM] = {
1, 2, 2, 4, 4, 4
};


You cannot have code outside of functions. (When do they get executed? What about any state it might rely on?)

EDIT:
Looks like this is inside a class, in which case:

// Foobar.h
class Foobar {
private:
static int block_state[BLOCK_TYPE_NUM];
};
...





Since you mentioned devC++ I feel compelled to urge you to use something better, their last release was in 2005! Microsoft Visual C++ Express is much better and free.


@dmatter

Thanks for the advice, I guess I do need to switch to a better compiler, I'm trying to avoid VC++ because I don't like linking in it, also I'm using Allegro 4 to make this game, do you know a tutorial on installing allegro 4 in code::blocks per chance?

Thanks again,

mathacka

This topic is closed to new replies.

Advertisement