Trivial (?) namespace problem

M

michael.d.pedersen

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.
 
M

MasterChief

Hy!
I think

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

should be

TestClass2::TestClass2(void)
{
}

try it.
 
M

michael.d.pedersen

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.
 
M

MasterChief

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

};

};
 
M

michael.d.pedersen

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.
 
B

Ben Voigt

wrote in message
Hi again,

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top