namespace problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eternalLearner
    New Member
    • May 2007
    • 35

    namespace problem

    hi all,

    i have a set of .cpp files and a header file ,i need to introduce a single namepace for these files.how do i do this ,i have not been able to figure out.
    thanks in advance.
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    #2
    I don't know about namespaces, but you could create a shared library with "ar"

    Comment

    • eternalLearner
      New Member
      • May 2007
      • 35

      #3
      hi,actually i have 2 set of codes ,which i have to add into one code.thus i require namespaces to solve the problem.Thus,ar will not help.
      Thanks for the help though.:)

      Comment

      • MACKTEK
        New Member
        • Mar 2008
        • 40

        #4
        Do either of the sets of code share the same names of functions or variables?

        If not, then it seems all you have to do is declare both set of files under the same namespace and then include them.
        The #pragma once tells the compiler not to use include if they were already included elsewhere...

        For example:
        // namespace_Forum .cpp : main project file.
        #pragma once
        #include "stdafx.h"
        #include "NS1.h"
        #using <mscorlib.dll >
        using namespace System;
        using namespace NS1;
        // void testfunction() {Console::Write Line("You are in testfunction, which is part of NS1.h");}

        namespace NS1 // partial -- more in a separate include
        {
        void testfunction2() {Console::Write Line("You are in testfunction2, which is part of the NS1 namespace, in file namespace_Forum .cpp");}
        }

        int main(array<Syst em::String ^> ^args)
        {
        Console::WriteL ine(L"Hello World");
        testfunction();
        testfunction2() ;
        Console::ReadLi ne();
        return 0;
        }

        // here is the other include
        // filename = NS1.h
        #pragma once
        #include "stdafx.h"
        #include "NS1.h"
        #using <mscorlib.dll >
        using namespace System;
        namespace NS1
        {
        void testfunction() {Console::Write Line("You are in testfunction, which is part of the NS1 namespace, in file NS1.h");}
        }

        // end of file

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

        You might have to use the scope operator :: to specify which namespace certain classes/functions etc belong to, if they have the same name.

        Comment

        • Studlyami
          Recognized Expert Contributor
          • Sep 2007
          • 464

          #5
          The #pragma once tells the compiler not to use include if they were already included elsewhere...
          be careful when using the #pragma because it is compiler dependent. The #pragma once is a MS way of doing the #ifndef, #define, ect. You can check out this thread where it was discussed a little bit more.

          Comment

          Working...