namespace problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vsgdp

    namespace problem

    I have a few variables that need to be global, but they are related and I
    thought it be cleaner to put them in a namespace:

    namespace x
    {
    A* a;
    B b;
    C c;
    }

    Several modules need access to this namespace, but I am getting a linker
    error

    "already defined in whatever.obj"

    This is similar to what happens if I used plain globals, but that is easily
    fixed by using extern and initializing the globals in one implementation
    file. I tried to do something similar here but it didn't work. Any
    solutions?



  • Gianni Mariani

    #2
    Re: namespace problem

    vsgdp wrote:[color=blue]
    > I have a few variables that need to be global, but they are related and I
    > thought it be cleaner to put them in a namespace:
    >
    > namespace x
    > {
    > A* a;
    > B b;
    > C c;
    > }
    >
    > Several modules need access to this namespace, but I am getting a linker
    > error
    >
    > "already defined in whatever.obj"
    >
    > This is similar to what happens if I used plain globals, but that is easily
    > fixed by using extern and initializing the globals in one implementation
    > file. I tried to do something similar here but it didn't work. Any
    > solutions?[/color]

    Try again - this below should work.

    ------- header file --------
    namespace xxx
    {
    extern int a;
    extern char foo[];
    };
    ----------------------------

    ------- implementation cpp file -------
    #include "header-file"

    namespace xxx
    {
    int a = 2;
    char foo[] = "ABCD";
    };
    ---------------------------------------




    Comment

    • vsgdp

      #3
      Re: namespace problem

      Most excellant. Thanks!


      Comment

      Working...