none
C2086 error defining a variable in a namespace.

    Question

  • Hi,

    I have put the following into my .h file.
    namespace MySpace
             {
                  bool started;
                  void Draw_screen(Form ^my_form);
                  . . .
             }

    As long as I only include functions it compiles fine.  But as soon as I try to put a variable into the definition I get errors
     error C2086: 'bool MySpace::started' : redefinition
     see declaration of 'MySpace::started

    I am using Studio 2008 and it is a CLR form/application I am using.
    Since I am not even using the new variable at this stage I do not see why I can't define it.

    Tks Zach.
    Friday, December 26, 2008 1:48 AM

Answers

  • Hi,

    This was solved, as the result of another thread.
    I had cyclic/recursive includes .    
    What I needed was forward declarations;

    eg

    Try like this

    // my_code.h

    namespace my_space
    {
       ref class Form1;  // forward declaration
    }

    namespace game_space
    {
      int var1;
    }



    // my_code.cpp

    #include "Form1.h"
    #include "my_code.h"

    //namespace gamespace implementation
    • Marked as answer by zach99 Monday, January 05, 2009 11:08 PM
    Monday, January 05, 2009 11:08 PM

All replies

  • Hmya, that's the trouble with trying to define, instead of declare, symbols in a header file.  Header files are usually #included more than once.  The second time the compiler sees a definition of "started", it goes WTF?   The old way to resolve that problem was the "extern" keyword.  The new way is to make global variables a static member of a class.
    Hans Passant.
    Friday, December 26, 2008 1:59 AM
  • Or add "#pragma once" at the beginnig of header ;) .
    Friday, December 26, 2008 1:17 PM
  • #pragma once won't help. If multiple translation units include the header, the program will violate the ODR.
    Friday, December 26, 2008 10:24 PM
  • I understand static variables variables in classes but I can not get this to work.
    I created a test CLR form and coded the following:
    ...
    #include "Form1.h"
    ...

    #ifndef zGlobalVars
    #define zGlobalVars  
    class   GlobalVars
            {
             public:
                    int    var1;
                    int    var2;
            };
    #endif


    namespace myspace
             {    void setup_fred();
                  GlobalVars  global_vars;
             }



    And in Form1.h I have the following
    #pragma once

    #include "my_code.h"
    . . .
        private: System::Void testingToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
                 {
                     myspace::global_vars.var1 = 1;      // <=== this is the error line
                 }
        };
    }


    BUT when I try to compile this I get the same style of error, same error id,  IE

    1>  error C2086: 'GlobalVars myspace::global_vars' : redefinition
    1>   see declaration of 'myspace::global_vars'

    As far I can see I am not redefine anything I am just trying to use it.

    Thanks Zach
    Tuesday, December 30, 2008 10:36 AM
  • Hi,

    This was solved, as the result of another thread.
    I had cyclic/recursive includes .    
    What I needed was forward declarations;

    eg

    Try like this

    // my_code.h

    namespace my_space
    {
       ref class Form1;  // forward declaration
    }

    namespace game_space
    {
      int var1;
    }



    // my_code.cpp

    #include "Form1.h"
    #include "my_code.h"

    //namespace gamespace implementation
    • Marked as answer by zach99 Monday, January 05, 2009 11:08 PM
    Monday, January 05, 2009 11:08 PM