Thread: some confusion about define global variable in c++

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    some confusion about define global variable in c++

    Dear friends:
    I am writing a compuational fluid dynamics code with c++, and the procedure have many sub-functions to compute different quantities.
    I want to defined a head file name as global.h to store all the globla variables, for example, the coordinates of grid points which will be used by ervery sub-functions.

    Code:
            global.h
              double x[][];
              double y[][];
               ............
              ................
               ..............
    hear is another file to compute the distance between differnet points.
    dis.cc
    ........
    ........
    ........


    another one to compute the volume of each control volume.
    vol.cc
    ......
    .............
    ...........

    the main file:

    Code:
         int main()
    {
    cout《x[][]<<y[][];
    }
    I tried to include the head file in each sub files, but it give me some error about "variabel re dine"
    could you please tell me how to use the globa variable defined in the global.h file in every sub-functions and the main file.

    Regards
    Yours Sincerely.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Put the word "extern" before all your global variables in the .h file.

    And make sure there is only ONE source file which defines each instance of the global variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    You could at least wrap those variables with a static class, to make it less "ugly", but your compiling problem probably lies in the fact that you don't use "include guards":
    Include guard - Wikipedia, the free encyclopedia
    Include guards - Cppwiki

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Quote Originally Posted by Xupicor View Post
    You could at least wrap those variables with a static class, to make it less "ugly", but your compiling problem probably lies in the fact that you don't use "include guards":
    Include guard - Wikipedia, the free encyclopedia
    Include guards - Cppwiki
    Thank you for you reply, dear friends.
    but if i use the include guide in the file, some sub function may not know the global variable defined in the header file.
    What should i do.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Quote Originally Posted by Salem View Post
    Put the word "extern" before all your global variables in the .h file.

    And make sure there is only ONE source file which defines each instance of the global variable.
    Dear frineds:
    thank you very much for your reply. you mean i need two head file. one wihout "external" added, and on with "external" added.?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ztdep
    you mean i need two head file. one wihout "external" added, and on with "external" added.?
    No. Use the extern keyword for the declaration in the header file. Don't use the extern keyword for the definition in exactly one source file.

    You should normally avoid the use of global variables.
    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

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Quote Originally Posted by laserlight View Post
    No. Use the extern keyword for the declaration in the header file. Don't use the extern keyword for the definition in exactly one source file.

    You should normally avoid the use of global variables.
    thank you very much for your reply. but my software have many variable to share between different subfunction. for example, the coordinates of each grid points will be used in every subfunctinos. Could you please give me some suggestion about how to avoid to use global variables.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ztdep
    my software have many variable to share between different subfunction. for example, the coordinates of each grid points will be used in every subfunctinos. Could you please give me some suggestion about how to avoid to use global variables.
    One fundamental technique is to make your functions have parameters so that you can pass arguments. If you find that you have a group of functions that operate on some global data, and another group of functions that operator on another set of global data (though these groups may overlap), then perhaps you should be thinking in terms of classes and objects.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers
    By MrMatt027 in forum C++ Programming
    Replies: 14
    Last Post: 12-10-2010, 04:32 PM
  2. USB1: Undefined reference to...
    By Oaktree in forum C Programming
    Replies: 6
    Last Post: 01-21-2010, 11:33 AM
  3. global variable confusion
    By agentsmith in forum C Programming
    Replies: 10
    Last Post: 01-30-2008, 02:25 PM
  4. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  5. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM