Sign in to follow this  
mathacka

Problem Initializing array in devc++

Recommended Posts

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:



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

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

Share this post


Link to post
Share on other sites
[code]
int block_states[BLOCK_TYPE_NUM] = {
1, 2, 2, 4, 4, 4
};
[/code]

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

EDIT:
Or:
[code]
// 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 );
}
[/code] Edited by fastcall22

Share this post


Link to post
Share on other sites
Since you mentioned devC++ I feel compelled to urge you to use something better, their last release was in 2005! [url="http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express"]Microsoft Visual C++ Express[/url] is much better and free.

Share this post


Link to post
Share on other sites
UPDATE

Got block_states to work by:

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

but now having a similar error as before with this similar code:
[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
[/CODE]

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

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

Later,

mathacka

Share this post


Link to post
Share on other sites
[quote name='mathacka' timestamp='1336768233' post='4939397']
[CODE]
BLOCK_POS[BLOCK_TYPE_NUM]; // expected unqualified-id before '[' token
[/CODE]
[/quote]
You haven't given it a variable name, e.g.:

[code]BLOCK_POS positions[BLOCK_TYPE_NUM];[/code]

Share this post


Link to post
Share on other sites
[quote name='fastcall22' timestamp='1336767090' post='4939385']
[code]
int block_states[BLOCK_TYPE_NUM] = {
1, 2, 2, 4, 4, 4
};
[/code]

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:
[code]
// Foobar.h
class Foobar {
private:
static int block_state[BLOCK_TYPE_NUM];
};
...
[/code]
[/quote]


[quote name='dmatter' timestamp='1336768224' post='4939396']
Since you mentioned devC++ I feel compelled to urge you to use something better, their last release was in 2005! [url="http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express"]Microsoft Visual C++ Express[/url] is much better and free.
[/quote]

@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 Edited by mathacka

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