global variables

This is a discussion on global variables within the C++ Programming forums, part of the General Programming Boards category; HI, I have two classes AppUI and operations in Appui.h -------------- class appui { ---------------------- Buf<KMaxTextLength> Body; } in operations.h ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    global variables

    HI,

    I have two classes

    AppUI and operations

    in Appui.h
    --------------
    class appui
    {
    ----------------------

    Buf<KMaxTextLength> Body;
    }

    in operations.h
    --------------------


    const TInt KMaxTextLength=100;

    class operations
    {
    -----------------------------

    Buf<KMaxTextLength> Body;
    }



    ----------------------------

    my problem is how to declare const TInt KMaxTextLength=100;

    when I declare it in one class and compile the code the other class shows error "KMaxTextLength not declared in this scope"

    how to solve this.
    Regards!
    Last edited by sandeepmhptr; 11-25-2007 at 07:28 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I think you should really declare the int in only one of the header files, and then declare the other var as extern. Include the headers in the right order in the .cpp files, and then redeclare the extern var again, except this time not as extern.....

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Use code tags: [code] code here [/code]

    In one of the header files, use
    Code:
    const TInt KMaxTextLength=100;
    and in the other, use
    Code:
    extern const TInt KMaxTextLength;
    [edit] As MacGyver said. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 01:59 AM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 03:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 09:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM

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