Global variables.

Discussion in 'C++' started by calcop@gmail.com, Jan 16, 2006.

  1. Guest

    Hello everyone,

    I hope I can write this question clearly. Anyway, it is about global
    variables. I have several files in my C++ project. One of which is
    main.cpp. This file comtains my main() function. I have a few other
    files in my project. They are server.cpp, server.h, strings.h,
    includes.h and functions.h. My main.cpp function #includes the
    includes.h file which includes strings.h and functions.h.

    My main() function fills a variable located win strings.h called
    sysName[32]. My main() function then calls a function in server.cpp.
    Becuase I delcared a prototype in server.h, it works. Now, my function
    in server.cpp cannot read my sysName variable even though #include
    "includes.h" is located in its header file.

    Why can't I access this variable. I think all the #includes are
    correct?

    I have been searching google and have been working on this for like 2
    hours now with no luck.

    Thanks for any help.
    , Jan 16, 2006
    #1
    1. Advertising

  2. Alan Johnson Guest

    wrote:
    > Hello everyone,
    >
    > I hope I can write this question clearly. Anyway, it is about global
    > variables. I have several files in my C++ project. One of which is
    > main.cpp. This file comtains my main() function. I have a few other
    > files in my project. They are server.cpp, server.h, strings.h,
    > includes.h and functions.h. My main.cpp function #includes the
    > includes.h file which includes strings.h and functions.h.
    >
    > My main() function fills a variable located win strings.h called
    > sysName[32]. My main() function then calls a function in server.cpp.
    > Becuase I delcared a prototype in server.h, it works. Now, my function
    > in server.cpp cannot read my sysName variable even though #include
    > "includes.h" is located in its header file.
    >
    > Why can't I access this variable. I think all the #includes are
    > correct?
    >
    > I have been searching google and have been working on this for like 2
    > hours now with no luck.
    >
    > Thanks for any help.
    >


    Variables should not be defined in header files. Pick one of your cpp
    files (or make a new one, for example, strings.cpp) and define it there.
    In your header file it should be declared (not defined) with the
    extern keyword. For example:

    extern char sysName[32] ;

    -Alan
    Alan Johnson, Jan 16, 2006
    #2
    1. Advertising

  3. Guest

    thank you
    , Jan 16, 2006
    #3
  4. On Mon, 16 Jan 2006 00:27:45 -0800, Alan Johnson
    <_edu> wrote:
    >
    >Variables should not be defined in header files. Pick one of your cpp
    >files (or make a new one, for example, strings.cpp) and define it there.
    > In your header file it should be declared (not defined) with the
    >extern keyword. For example:
    >
    >extern char sysName[32] ;


    Better avoid any global variables and pass argumnents to functions.

    Best wishes,
    Roland Pibinger
    Roland Pibinger, Jan 16, 2006
    #4
  5. Zara Guest

    On 15 Jan 2006 23:52:42 -0800, wrote:

    >Hello everyone,
    >
    >I hope I can write this question clearly. Anyway, it is about global
    >variables. I have several files in my C++ project. One of which is
    >main.cpp. This file comtains my main() function. I have a few other
    >files in my project. They are server.cpp, server.h, strings.h,
    >includes.h and functions.h. My main.cpp function #includes the
    >includes.h file which includes strings.h and functions.h.
    >
    >My main() function fills a variable located win strings.h called
    >sysName[32]. My main() function then calls a function in server.cpp.
    >Becuase I delcared a prototype in server.h, it works. Now, my function
    >in server.cpp cannot read my sysName variable even though #include
    >"includes.h" is located in its header file.
    >
    >Why can't I access this variable. I think all the #includes are
    >correct?
    >
    >I have been searching google and have been working on this for like 2
    >hours now with no luck.
    >
    >Thanks for any help.


    You have submitted incomplete information...

    But, anycase, I think there may be two possibilities:

    a) In strings. h you have a declaration like "extern char
    sysName[32];", but you have not defined sysyName anywhere. Thus, you
    will have a linker error.

    b) (MOST PROBABLE) In strings.h you have a definition like "char
    sysyName[32];". This way, you will have a global sysName for each
    unit that includes string.h. The linekr may (or may not) giove some
    warning about duplicate global name, but the syysName seen from main
    and from server are different ones, so they will not communicate at
    all.

    Solution, in both cases:

    In strings.h, declare the variable: extern char sysName[32];
    Create and include in the project, a module strings.cpp, with the
    definition: char sysnName[32];

    Then all modules will refer to the same sysName


    If your problem is not one of those outlines above, please submit
    significant pieces of code to identify the real problem.

    Best regards,

    Zara

    PS: Someone in this list mayl flame you for using global variables
    instead of, for instance, a Singleton class or something alike. The
    solution I give is intende as the fastest to implement, not the mos
    elegant/smartest one.
    Zara, Jan 16, 2006
    #5
  6. Guest

    Thanks everyone, Alan was right on the money and it worked. Thanks
    again.
    , Jan 16, 2006
    #6
  7. Ian Collins Guest

    wrote:
    > Hello everyone,
    >
    > I hope I can write this question clearly. Anyway, it is about global
    > variables. I have several files in my C++ project. One of which is
    > main.cpp. This file comtains my main() function. I have a few other
    > files in my project. They are server.cpp, server.h, strings.h,
    > includes.h and functions.h. My main.cpp function #includes the
    > includes.h file which includes strings.h and functions.h.
    >
    > My main() function fills a variable located win strings.h called
    > sysName[32]. My main() function then calls a function in server.cpp.
    > Becuase I delcared a prototype in server.h, it works. Now, my function
    > in server.cpp cannot read my sysName variable even though #include
    > "includes.h" is located in its header file.
    >
    > Why can't I access this variable. I think all the #includes are
    > correct?
    >

    One way is to declare sysName (as a string) as extern in the header and
    define it in one of your .cpp files.

    strings.h:

    extern std::string sysName;

    main.cpp:

    std::string sysName;

    --
    Ian Collins.
    Ian Collins, Jan 16, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne
    Replies:
    2
    Views:
    470
    Wayne
    Nov 11, 2003
  2. jubelbrus
    Replies:
    5
    Views:
    616
    JohnQ
    Jul 20, 2007
  3. mark4asp
    Replies:
    1
    Views:
    217
  4. Tony Archer

    Global Variables? Multi-Application Variables?

    Tony Archer, Nov 24, 2003, in forum: ASP General
    Replies:
    5
    Views:
    223
    Tony Archer
    Nov 25, 2003
  5. king
    Replies:
    2
    Views:
    170
    Tad McClellan
    Jun 27, 2006
Loading...

Share This Page