views:

94

answers:

2

My code was working fine, until I tried to wrap all of my class definitions in a namespace.

// "Player.h"
#include "PhysicsObject.h"
namespace MaelstromII
{
    class Player : public MaelstromII::PhysicsObject
    {
        // ...
    };
}

// "PhysicsObject.h"
#include "GameObject.h"
namespace MaelstromII
{
    class PhysicsObject : public MaelstromII::GameObject
    {
        // ...
    };
}

// "GameObject.h"
namespace MaelstromII
{
    class GameObject
    {
        // ...
    };
}

When I compile in Visual Studio, I get a bunch of these errors:

error C2039: 'PhysicsObject' : is not a member of 'MaelstromII'

It complains about GameObject, too.

Does anyone know why this is?

+2  A: 

I'm not 100%, but I think what's going on when you say

namespace Foo
{
    class Bar : public Foo::BarBase {}
}

is the same as:

class Foo::Bar : public Foo::Foo::BarBase {}

When you're in a namespace, you don't need to use the namespace:: specifier to access other things in that namespace.

Narfanator
True, but it's not going to cause an error either since it will keep going up the namespace hierarchy looking for `Foo::BarBase` and will find it relative to the top-level nemaspace.
Troubadour
Aha? Learn something new every day. Thanks!
Narfanator
A: 

Turns out the trouble was caused by a circular dependency in my code somewhere else. After fixing that problem, my code compiled fine.

Evidently, there is no difference between this:

namespace Foo {
    class Bar {}
    class Bar2 : public Bar {}
}

And this:

namespace Foo {
    class Bar {}
    class Bar2 : public Foo::Bar {}
}

The compiler resolves them the same way.

Achris