multiple files

This is a discussion on multiple files within the C++ Programming forums, part of the General Programming Boards category; My program uses several global variables to handle things that a lot of functions will be using and changing ( ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    8

    multiple files

    My program uses several global variables to handle things that a lot of functions will be using and changing ( HDCs, HWNDs, etc ) The unfortunate thing is that these functions are spread across many differant files. How can I declare a global variable in a header file without redefinition errors?
    Last edited by YingPar; 06-04-2003 at 07:04 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Use inclusion guards with the preprocessor:
    Code:
    #ifndef DEFS_H_
    #define DEFS_H_
    
    // Definitions go here
    
    #endif
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You can declare global variables in a .cpp file like this:

    double pi=3.14;

    and then use

    extern int pi;

    in the other .cpp files. If your globals are declared as const, then they must be declared with the extern key word as well, like:

    extern const double pi = 50;

    so they are accessible from another .cpp file:

    extern const double pi;

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    8
    Thanks, it seems to be working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  2. need assistance with multiple files
    By c++.prog.newbie in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2006, 01:44 AM
  3. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 11:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 10:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21