Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2006

Tip: Looking for answers? Try searching our database.

Trivial (?) namespace problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
michael.d.pedersen@googlemail.com - 23 Nov 2006 11:03 GMT
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::TestClass2^ 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::TestClass1::TestClass1(void)
{
}

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

Test::TestClass2::TestClass2(void)
{
}

--------------- 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 - 23 Nov 2006 11:34 GMT
Hy!
I think

Test::TestClass2::TestClass2(void)
{
}

should be

TestClass2::TestClass2(void)
{
}

try it.

> Hello group,
>
[quoted text clipped - 63 lines]
> Thanks,
> Michael.
michael.d.pedersen@googlemail.com - 23 Nov 2006 13:36 GMT
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.

> Hy!
> I think
[quoted text clipped - 78 lines]
> > Thanks,
> > Michael.
MasterChief - 23 Nov 2006 13:58 GMT
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::TestClass2 t2; // <-- results in error

};

};

> Hello,
>
[quoted text clipped - 90 lines]
>> > Thanks,
>> > Michael.
michael.d.pedersen@googlemail.com - 23 Nov 2006 15:10 GMT
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.
Ben Voigt - 24 Nov 2006 15:27 GMT
wrote in message
news:1164294605.448111.29330@h54g2000cwb.googlegroups.com...
>Hi again,
>
[quoted text clipped - 3 lines]
>> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.