Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problem Initializing array in devc++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 mathacka   Members   -  Reputation: 124

Like
0Likes
Like

Posted 11 May 2012 - 02:06 PM

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

Sponsor:

#2 fastcall22   Crossbones+   -  Reputation: 4158

Like
0Likes
Like

Posted 11 May 2012 - 02:11 PM

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 );
}

Edited by fastcall22, 11 May 2012 - 02:18 PM.

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#3 dmatter   Crossbones+   -  Reputation: 3013

Like
0Likes
Like

Posted 11 May 2012 - 02:30 PM

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.

#4 mathacka   Members   -  Reputation: 124

Like
0Likes
Like

Posted 11 May 2012 - 02:30 PM

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

#5 dmatter   Crossbones+   -  Reputation: 3013

Like
0Likes
Like

Posted 11 May 2012 - 02:34 PM

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];


#6 mathacka   Members   -  Reputation: 124

Like
0Likes
Like

Posted 11 May 2012 - 02:35 PM

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

Edited by mathacka, 11 May 2012 - 02:36 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS