CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2011
    Posts
    5

    global variable not part of namespace

    So the issue is ive declared a variable in one file and called it in another like so:

    File1.cpp:
    classobject *var1;

    File2.cpp
    ::var1->Dofunction();


    This returns:

    'var1' : is not a member of '`global namespace''


    I'm still a little new to this so it might just be the syntax here.

    Thanks in advance

  2. #2
    Join Date
    Jun 2011
    Posts
    5

    Re: global variable not part of namespace

    Can the mod delete this post im gonna move it to another index.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: global variable not part of namespace

    Quote Originally Posted by Scubastooge View Post
    Can the mod delete this post im gonna move it to another index.
    What is an "index", and why should the moderator delete the post? You're asking a C++ question, so which forum do you think it belongs in?

    Second, I don't see any declaration of "var1" in File2.cpp, so the error is obvious. A C++ source module such as File2.cpp has no idea that you've declared some variable in File1.cpp. A C++ compiler only looks at the definitions in the current moducle it's compiling.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jun 2011
    Posts
    5

    Re: global variable not part of namespace

    var1 is global scoped though. shouldn't i be able to access it from any file?

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: global variable not part of namespace

    Yes but as Paul said it has to be declared so the compiler knows what var1 is. Either you add a header and include that or you just declare manually
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: global variable not part of namespace

    Quote Originally Posted by Scubastooge View Post
    var1 is global scoped though. shouldn't i be able to access it from any file?
    When you compile a source module, the compiler only knows what it is currently in the file being compiled. The compiler knows nothing about what you did in another file.

    To let the compiler know that there is a variable somewhere out there called "var1", you must tell the this to the compiler. To do that, you use the extern keyword to inform the compiler that "there is a variable called var1 out there somewhere, not declared in this file, but in another file, so it's OK to use it here".

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 11th, 2011 at 05:27 PM.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: global variable not part of namespace

    Note also that you will need to initialize that pointer before you can use it anywhere, much less in a different file.

  8. #8
    Join Date
    Dec 2010
    Posts
    20

    Re: global variable not part of namespace

    add "extern classobject *var1;" in file2.cpp will be ok.


    using global variables in cpp is a very bad behavior!!
    you should use the singleton pattern instead.

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: global variable not part of namespace

    Quote Originally Posted by deyili View Post
    using global variables in cpp is a very bad behavior!!
    you should use the singleton pattern instead.
    While I agree that using global variables is often bad design, using singletons as glorified globals is not much better. You should design to remove the need for static or global data, not just replace an evil with a lesser evil.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  10. #10
    Join Date
    Dec 2010
    Posts
    20

    Re: global variable not part of namespace

    Quote Originally Posted by D_Drmmr View Post
    While I agree that using global variables is often bad design, using singletons as glorified globals is not much better. You should design to remove the need for static or global data, not just replace an evil with a lesser evil.
    yes, you are right. but how do we do when global data sharing is inevitable ?

  11. #11
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: global variable not part of namespace

    Quote Originally Posted by deyili View Post
    yes, you are right. but how do we do when global data sharing is inevitable ?
    I can't imagine a situation where global variables are inevitable, but in certain cases they may be the best option. In such cases, a singleton may be a good design, but it is not a "one size fits all" solution.
    The point is that merely replacing global variables with singletons does not address the design flaw that mostly underlies the use of globals.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured