namespace difficulties (many files)

This is a discussion on namespace difficulties (many files) within the C++ Programming forums, part of the General Programming Boards category; How can I get namespaces to span multiple files? If I do a namespace blah { // .... } in ...

  1. #1
    Unregistered
    Guest

    namespace difficulties (many files)

    How can I get namespaces to span multiple files?

    If I do a

    namespace blah
    {
    // ....
    }

    in one file, then try and use it in another I get compiler errors that it doesn't know what the namespace is, and I've tried preceeding it with extern, and it's contained variables.

    If I define the namespace in a header, the compiler gives me multiple definition errors. If I define the skeleton in a header and the variables in a .cpp file, they are not seen in the different files! please help, it's driving me nuts!

    -S

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Are you including the files in the build aswell as including them with the include statement in another file?

    What compiler are you using?
    zen

  3. #3
    Unregistered
    Guest
    I'm using VC++ 6, SP 4

    I have tested protecting my #includes with #pragma once and with my own #defines (#ifndef SOMETHING, #define, #endif etc...) and I still get errors - if you could write a very small bit of code to show me how it should be done, or something I can at least test to see if it is something beyond my control I would be grateful.

    If not, just any help will be much appreciated

    -S

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you put the namespaces in your header files, right click on them in file view, select settings/general and tick the box 'exclude from build'. Then include them in the files they are required using the include directive as you would standard library header files and everything should link.
    zen

  5. #5
    Unregistered
    Guest

    Unhappy

    I did that and it's still throwing up errors - here is the brief code for what I've got right now (just to try and make it work):

    in the file something.h

    namespace my_ns
    {
    int blah_a;
    int blah_b;
    }

    in the file someone.cpp

    include "something.h"

    // code, using my_ns variables like my_ns::blah_b

    in the file sometwo.cpp

    include "something.h"

    // code, using my_ns vars like my_ns::blah_a or whatever

    I've tried doing what you said (excluding something.h from build)
    I've tried using #pragma once
    I've tried using the #ifndef, #define... thing

    All I want is a namespace declared in one file to be accessible in another and it's just not happening!
    I simply fail to understand it.

    Thanks for your help so far zen

    -S

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    There is probably an easier way than this but I've tried it and it works and I can't seem to prevent re-definition any other way -

    1) Create a static library in MSVC.

    2) In the header in the static library put your namespace and variable declaration -

    namespace test
    {
    extern int a;
    }

    3) In the implementation file of your library, initialise your variable to whatvever value you require (don't forget to include the header file) -

    int test::a=10;

    4) In your project include the library header file where it's needed and in project settings link to the file created when your library was compiled (I just put the header and library in the standard include/lib paths).
    zen

  7. #7
    Unregistered
    Guest
    Thanks Zen,

    I'll be sure to try that - It will probably work, but as you said, there is probably an easier way. I will try to find it, as static libraries are not my favourite tool in the box.

    Regards,

    -S

  8. #8
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Sorry, just use a header file in your project with the namespace and extern'd variable and another implementation file that includes this header and defines the variable, then include the header where it's needed

    Code:
    //namespace header
    #include "stdafx.h"
    
    #ifndef name_h
    #define name_h
    
    namespace test
    {
    	extern int a;
    }
    
    #endif
    Code:
    //namespace imp
    #include "stdafx.h"
    #include "name.h"
    
    int test::a=10;
    I'm sure I already tried this but it's working now
    zen

  9. #9
    Unregistered
    Guest
    outstanding, thanks zen.

    Do I need to somehow define storage for the extern somewhere in a .cpp, or can I just use it straight away?

  10. #10
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The storage is defined in the second file (namespace imp in my example). Include both these files as part of your project, but only include the namespace header in the files that the namespace is needed.
    zen

  11. #11
    Unregistered
    Guest
    I don't actually use stdafx.h, but I see where you are coming from

    Cheers,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 07:55 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 03:47 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM

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