How do I define the index within the array?

#define array index not working

  • 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! }

  • Answer:

    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.

Daqs at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

When you define the symbol in this way: #define MAX_TEAMS = 20; you define MAX_TEAMS to be replaced with = 20;

tobi303

Change #define MAX_TEAMS = 20; to #define MAX_TEAMS 20 #define does a string substitution.

Ed Heal

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.