some confusion about define global variable in c++

This is a discussion on some confusion about define global variable in c++ within the C++ Programming forums, part of the General Programming Boards category; Dear friends: I am writing a compuational fluid dynamics code with c++, and the procedure have many sub-functions to compute ...

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

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    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
    5
    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
    5
    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
    20,971
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    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
    20,971
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:32 PM
  2. USB1: Undefined reference to...
    By Oaktree in forum C Programming
    Replies: 6
    Last Post: 01-21-2010, 10:33 AM
  3. global variable confusion
    By agentsmith in forum C Programming
    Replies: 10
    Last Post: 01-30-2008, 01: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

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