Thread: External Variables

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    9

    External Variables

    I've been having a bit of trouble with external variables, and I was wondering whether anyone had an idea why this doesn't compile. (This is just example code, though it has the same errors).
    Code:
    //GLOBALS.H////////////////////////
    #pragma once
    
    extern const int MAPH;
    extern const int MAPW;
    extern const int CELLSIZE;
    ////////////////////////////////////////
    //GLOBALS.CPP////////////////////
    #include "globals.h"
    
    const int MAPH = 40;
    const int MAPW = 60;
    const int CELLSIZE = 10;
    ///////////////////////////////////////
    //FOO.H/////////////////////////////
    #pragma once
    #include "globals.h"
    
    void foo(int arr[][MAPH]);
    ///////////////////////////////////////
    //FOO.CPP/////////////////////////
    #include "globals.h"
    #include "foo.h"
    
    void foo(int arr[][MAPH])
    {
    //do whatever
    }
    //////////////////////////////////////
    //MAIN.CPP////////////////////////
    #include "globals.h"
    #include "foo.h"
    
    int main()
    {
            int arr[MAPW][MAPH];
            foo(arr);
            return 0;
    }
    //////////////////////////////////////
    As usuall, any help at all appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Raptoricus
    I've been having a bit of trouble with external variables, and I was wondering whether anyone had an idea why this doesn't compile. (This is just example code, though it has the same errors).
    You might want to state what those errors are.

    The problem is probably because the value of MAPH is not known when compiling foo.cpp, and the values of MAPH and MAPW are not known when compiling main.cpp, so it is as if you were trying to use variable length arrays. They are only known when linking the object file generated by compiling globals.cpp, but by then it is too late.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    9
    Quote Originally Posted by laserlight View Post
    You might want to state what those errors are.
    Not a problem:
    variable-size type declared outside of any function <- this appears in a functions header file (the function is practically the same as foo, with a multi dimensional array, using a global variable for bounds)

    What you're saying makes sense, but I'm not too sure how to fix the problem, any advice?
    Last edited by Raptoricus; 07-19-2009 at 01:38 AM. Reason: Tested

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Raptoricus
    What you're saying makes sense, so would moving the includes around fix the problem?
    Not likely, and generally requiring a specific order of inclusion leads to fragile code and so is a Bad Thing.

    One way out, with minimal change to your current code, would be to use an enumeration in globals.h and get rid of globals.cpp:
    Code:
    enum {MAPH = 40, MAPW = 60, CELLSIZE = 10};
    Incidentally, note that fully capitalised names are typically reserved for macro names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    9
    Okay thanks, I'll try that .

    Quote Originally Posted by laserlight View Post
    Incidentally, note that fully capitalised names are typically reserved for macro names.
    Oh my bad, my tutor says to use something like"const int kVARIABLE", to be honest though, I've just been trying to get the code to work, and not worrying so much about style. Still bad style leads to bad code I suppose, so thanks for pointing it out.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    9
    It worked! Thanks very much for your help.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    All-caps is very common for constants, simply because constants in C are macros and thus capitalized. The convention simply carried over to C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. Overloaded Operator Causes Unresolved External Symbol
    By mikeman118 in forum C++ Programming
    Replies: 11
    Last Post: 03-03-2008, 04:40 PM
  4. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  5. external variables
    By schlamniel in forum C Programming
    Replies: 1
    Last Post: 11-02-2002, 05:40 AM