Thread: variable-size type declared outside of any function

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    variable-size type declared outside of any function

    ^ that is the error i get ^

    i am declaring 2 global variables:

    PHP Code:
    // Global Variables
    int MAXPLAYERS;
    int players[MAXPLAYERS+1]; 
    i need these global because i have my program in all different functions, and they all must access the players[] array. Does anyone have any ideas about what i could do?

    [INFO]
    I am making a program that helps with tournaments ( Lets the user enter all the data and the program seeds them and displays pairings )

    I am using the MinGW Compiler (with the command line)

    I am running Windows 98 SE, but in a couple minutes i will install Windows ME
    [/INFO]

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >int MAXPLAYERS;
    >int players[MAXPLAYERS+1];

    Is it valid to declare players like this? Shouldn't be the size of the array a constant?

    If you declare these variables global, then there available to all functions just by using their names.


    Code:
    file 1:
    
    int global_variable;
    
    void function (void)
    {
        global_variable = 1;
    }
    
    
    file 2:
    
    extern int global_variable;
    
    void other_function (void)
    {
        printf ("Value of global_variable is %d\n", global_variable);
    }

  3. #3
    Unregistered
    Guest
    change this:
    Code:
    int MAXPLAYERS
    to this:
    Code:
    const int MAXPLAYERS = 100;  //for example
    Also, why would you have a variable called MAXPLAYERS then add 1 to it for the array, shouldnt it be MAXPLAYERSMINUSONE :P

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    16
    You haven't defined the size of MAXPLAYERS, so [MAXPLAYERS
    +1] isn't defined either. The stuff in square brackets is
    the array dimensions, and those have to be set whenever
    you declare the array. It looks like what you're trying
    to do is create an array that changes in size, and as far
    as I know, that is illegal in C++.

    Miki
    [email protected]

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    >>Also, why would you have a variable called MAXPLAYERS then add 1 to it for the array, shouldnt it be MAXPLAYERSMINUSONE :P

    no, i will just disregard the players[0]; because i dont want there to be a #0 Player so i add one to the array so i can have 1 to MAXPLAYERS instad of 0 to MAXPLAYERS-1

    I need the user to enter how many players will be playing (MAXPLAYERS) and want that array to size it's self according to that
    Last edited by Okiesmokie; 03-23-2002 at 02:16 PM.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Would a vector work in this situation?

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    16
    Originally posted by Okiesmokie

    I need the user to enter how many players will be playing (MAXPLAYERS) and want that array to size it's self according to that [/B]
    Then you need to have the input routine before you declare
    the array. As I said before, you can't have an
    unspecified array size upon declaration. If the compiler
    doesn't know how big the array is at the time the array is
    declared, it will tell you so and refuse to compile.

    Miki
    ustuzou%

  8. #8
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Yeah.. std::vector is the easiest way to do this.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM