Trivial (?) namespace problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michael.d.pedersen@googlemail.com

    Trivial (?) namespace problem

    Hello group,

    I am new to Visual C++ 8 and have a seemingly trivial problem using
    namespaces. The scenario: I have two classes, TestClass1 and
    TestClass2, in the namespace Test. I would like to create a reference
    to TestClass2 in TestClass1; but the compiler doesn't seem to recognize
    the namespace Test and any classes in it.

    Here are my class definitions and declarations:

    ---------- TestClass1.h -------------
    #pragma once
    #include "TestClass2 .h"

    namespace Test
    {
    public ref class TestClass1
    {
    public:
    TestClass1(void );
    Test::TestClass 2^ t2; // <-- results in error
    };
    };


    ----------------- TestClass2.h --------------
    #pragma once
    #include "TestClass1 .h"

    namespace Test
    {
    public ref class TestClass2
    {
    public:
    TestClass2(void );
    };
    };


    -------------- TestClass1.cpp ------------------
    #include "StdAfx.h"
    #include "TestClass1 .h"

    Test::TestClass 1::TestClass1(v oid)
    {
    }


    -------------- TestClass2.cpp ------------------
    #include "StdAfx.h"
    #include "TestClass2 .h"

    Test::TestClass 2::TestClass2(v oid)
    {
    }

    --------------- end of code ----------------------

    Compilation results in the following: "error C2039: 'TestClass2' : is
    not a member of 'Test'".
    If I include "using namespace Test" in either of the header files, I
    get an error saying that the namespace Test does not exist. However,
    Intellisense does list TestClass2 as a member of Test, and the
    namespace Test is listed in the object browser.

    I must be overlooking something embarasingly simple here. Any
    suggestions would be much appreciated.

    Thanks,
    Michael.

  • MasterChief

    #2
    Re: Trivial (?) namespace problem

    Hy!
    I think

    Test::TestClass 2::TestClass2(v oid)
    {
    }

    should be

    TestClass2::Tes tClass2(void)
    {
    }

    try it.

    <michael.d.pede rsen@googlemail .comschrieb im Newsbeitrag
    news:1164279833 .108453.209940@ l39g2000cwd.goo glegroups.com.. .
    Hello group,
    >
    I am new to Visual C++ 8 and have a seemingly trivial problem using
    namespaces. The scenario: I have two classes, TestClass1 and
    TestClass2, in the namespace Test. I would like to create a reference
    to TestClass2 in TestClass1; but the compiler doesn't seem to recognize
    the namespace Test and any classes in it.
    >
    Here are my class definitions and declarations:
    >
    ---------- TestClass1.h -------------
    #pragma once
    #include "TestClass2 .h"
    >
    namespace Test
    {
    public ref class TestClass1
    {
    public:
    TestClass1(void );
    Test::TestClass 2^ t2; // <-- results in error
    };
    };
    >
    >
    ----------------- TestClass2.h --------------
    #pragma once
    #include "TestClass1 .h"
    >
    namespace Test
    {
    public ref class TestClass2
    {
    public:
    TestClass2(void );
    };
    };
    >
    >
    -------------- TestClass1.cpp ------------------
    #include "StdAfx.h"
    #include "TestClass1 .h"
    >
    Test::TestClass 1::TestClass1(v oid)
    {
    }
    >
    >
    -------------- TestClass2.cpp ------------------
    #include "StdAfx.h"
    #include "TestClass2 .h"
    >
    Test::TestClass 2::TestClass2(v oid)
    {
    }
    >
    --------------- end of code ----------------------
    >
    Compilation results in the following: "error C2039: 'TestClass2' : is
    not a member of 'Test'".
    If I include "using namespace Test" in either of the header files, I
    get an error saying that the namespace Test does not exist. However,
    Intellisense does list TestClass2 as a member of Test, and the
    namespace Test is listed in the object browser.
    >
    I must be overlooking something embarasingly simple here. Any
    suggestions would be much appreciated.
    >
    Thanks,
    Michael.
    >

    Comment

    • michael.d.pedersen@googlemail.com

      #3
      Re: Trivial (?) namespace problem

      Hello,

      Thanks for your suggestion. However, if I remove the Test::-qualifier
      here I get the following error:

      'TestClass2' : is not a class or namespace

      (that's why I put it there in the first place).

      Best,
      Michael.

      MasterChief wrote:
      Hy!
      I think
      >
      Test::TestClass 2::TestClass2(v oid)
      {
      }
      >
      should be
      >
      TestClass2::Tes tClass2(void)
      {
      }
      >
      try it.
      >
      <michael.d.pede rsen@googlemail .comschrieb im Newsbeitrag
      news:1164279833 .108453.209940@ l39g2000cwd.goo glegroups.com.. .
      Hello group,

      I am new to Visual C++ 8 and have a seemingly trivial problem using
      namespaces. The scenario: I have two classes, TestClass1 and
      TestClass2, in the namespace Test. I would like to create a reference
      to TestClass2 in TestClass1; but the compiler doesn't seem to recognize
      the namespace Test and any classes in it.

      Here are my class definitions and declarations:

      ---------- TestClass1.h -------------
      #pragma once
      #include "TestClass2 .h"

      namespace Test
      {
      public ref class TestClass1
      {
      public:
      TestClass1(void );
      Test::TestClass 2^ t2; // <-- results in error
      };
      };


      ----------------- TestClass2.h --------------
      #pragma once
      #include "TestClass1 .h"

      namespace Test
      {
      public ref class TestClass2
      {
      public:
      TestClass2(void );
      };
      };


      -------------- TestClass1.cpp ------------------
      #include "StdAfx.h"
      #include "TestClass1 .h"

      Test::TestClass 1::TestClass1(v oid)
      {
      }


      -------------- TestClass2.cpp ------------------
      #include "StdAfx.h"
      #include "TestClass2 .h"

      Test::TestClass 2::TestClass2(v oid)
      {
      }

      --------------- end of code ----------------------

      Compilation results in the following: "error C2039: 'TestClass2' : is
      not a member of 'Test'".
      If I include "using namespace Test" in either of the header files, I
      get an error saying that the namespace Test does not exist. However,
      Intellisense does list TestClass2 as a member of Test, and the
      namespace Test is listed in the object browser.

      I must be overlooking something embarasingly simple here. Any
      suggestions would be much appreciated.

      Thanks,
      Michael.

      Comment

      • MasterChief

        #4
        Re: Trivial (?) namespace problem

        Uups!
        Sorry I haven´t read your message carefully. The problem is that the
        compiler does´n´t know TestClass2 at this time.
        You have to tell him that this class will be decalred later. Just insert
        class TestClass2; before declaration of TestClass1.
        Like this:

        namespace Test

        {

        class TestClass2;

        class TestClass1

        {

        public:

        TestClass1(void );

        Test::TestClass 2 t2; // <-- results in error

        };

        };

        <michael.d.pede rsen@googlemail .comschrieb im Newsbeitrag
        news:1164289016 .476946.66260@l 39g2000cwd.goog legroups.com...
        Hello,
        >
        Thanks for your suggestion. However, if I remove the Test::-qualifier
        here I get the following error:
        >
        'TestClass2' : is not a class or namespace
        >
        (that's why I put it there in the first place).
        >
        Best,
        Michael.
        >
        MasterChief wrote:
        >Hy!
        >I think
        >>
        >Test::TestClas s2::TestClass2( void)
        >{
        >}
        >>
        >should be
        >>
        >TestClass2::Te stClass2(void)
        >{
        >}
        >>
        >try it.
        >>
        ><michael.d.ped ersen@googlemai l.comschrieb im Newsbeitrag
        >news:116427983 3.108453.209940 @l39g2000cwd.go oglegroups.com. ..
        Hello group,
        >
        I am new to Visual C++ 8 and have a seemingly trivial problem using
        namespaces. The scenario: I have two classes, TestClass1 and
        TestClass2, in the namespace Test. I would like to create a reference
        to TestClass2 in TestClass1; but the compiler doesn't seem to recognize
        the namespace Test and any classes in it.
        >
        Here are my class definitions and declarations:
        >
        ---------- TestClass1.h -------------
        #pragma once
        #include "TestClass2 .h"
        >
        namespace Test
        {
        public ref class TestClass1
        {
        public:
        TestClass1(void );
        Test::TestClass 2^ t2; // <-- results in error
        };
        };
        >
        >
        ----------------- TestClass2.h --------------
        #pragma once
        #include "TestClass1 .h"
        >
        namespace Test
        {
        public ref class TestClass2
        {
        public:
        TestClass2(void );
        };
        };
        >
        >
        -------------- TestClass1.cpp ------------------
        #include "StdAfx.h"
        #include "TestClass1 .h"
        >
        Test::TestClass 1::TestClass1(v oid)
        {
        }
        >
        >
        -------------- TestClass2.cpp ------------------
        #include "StdAfx.h"
        #include "TestClass2 .h"
        >
        Test::TestClass 2::TestClass2(v oid)
        {
        }
        >
        --------------- end of code ----------------------
        >
        Compilation results in the following: "error C2039: 'TestClass2' : is
        not a member of 'Test'".
        If I include "using namespace Test" in either of the header files, I
        get an error saying that the namespace Test does not exist. However,
        Intellisense does list TestClass2 as a member of Test, and the
        namespace Test is listed in the object browser.
        >
        I must be overlooking something embarasingly simple here. Any
        suggestions would be much appreciated.
        >
        Thanks,
        Michael.
        >
        >

        Comment

        • michael.d.pedersen@googlemail.com

          #5
          Re: Trivial (?) namespace problem

          Hi again,
          Sorry I haven´t read your message carefully. The problem is that the
          compiler does´n´t know TestClass2 at this time.
          You have to tell him that this class will be decalred later. Just insert
          class TestClass2; before declaration of TestClass1.
          Like this:
          Thanks, that did the trick! I thought forward declarations were part of
          the past, but apparently not (o:

          Best,
          Michael.

          Comment

          • Ben Voigt

            #6
            Re: Trivial (?) namespace problem


            wrote in message
            news:1164294605 .448111.29330@h 54g2000cwb.goog legroups.com...
            >Hi again,
            >
            >Sorry I haven´t read your message carefully. The problem is that the
            >compiler does´n´t know TestClass2 at this time.
            >You have to tell him that this class will be decalred later. Just insert
            >class TestClass2; before declaration of TestClass1.
            >Like this:
            That will work until you actually try to start using the class, then you'll
            also
            want to #include both header files in each implementation (.cpp) file.
            >
            >Thanks, that did the trick! I thought forward declarations were part of
            >the past, but apparently not (o:
            Not at all. It's just that several increasingly popular languages (Java,
            C#) use multi-pass compilation and don't have forward declarations. They
            pay a price in complexity, you could never get C++'s template behavior
            w.r.t. local specializations in that way. Also, it's useful for minimal
            rebuild -- C# must recompile every single file after each change, while C++
            needn't.
            >
            >Best,
            >Michael.

            Comment

            Working...