1

I'm trying to #define a constant at the top of the header file and use that value as the index size of my arrays. I'm getting the following error:

Error C2059: syntax error : ']'

I'm curious as to why?

#define MAX_TEAMS = 20; class Program { public: int atk_val[MAX_TEAMS]; // Error! int atk_val[20]; // Works! } 
3
  • 8
    static const int MAX_TEAMS = 20; instead of #define Commented Jul 10, 2015 at 11:17
  • 4
    please note that you should better use a static const as Captiain Obvlious pointed out, or even better use standard containers instead of plain arrays Commented Jul 10, 2015 at 11:19
  • and btw you are defining the size of the array and not its index. (yes sometimes I like hairsplitting). If your program has no bugs, the index should never reach this value ;) Commented Jul 10, 2015 at 11:21

3 Answers 3

4

Replace #define MAX_TEAMS = 20; with

#define MAX_TEAMS 20 

In current form the code int atk_val[MAX_TEAMS]; will be expanded to

int atk_val[= 20;]; // ^ ^ 

And thus the error message seen by you.

Always remember that MACROs are not variables.

Sign up to request clarification or add additional context in comments.

Comments

1

When you define the symbol in this way:

#define MAX_TEAMS = 20; 

you define MAX_TEAMS to be replaced with = 20;

Comments

1

Change

 #define MAX_TEAMS = 20; 

to

#define MAX_TEAMS 20 

#define does a string substitution.

1 Comment

Please see the comment above - it is a better solution to mine in the C++ world.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.