ibrabeicker
ibrabeicker

Reputation: 1839

How can I resolve this namespace issue?

I have this following scenario

    namespace A {
    //...
    class A {
        public:
        static void foo();
    }
    //...
    } //namespace A

When I try to access foo() from another class with A::foo() the compiler complains about "foo is not a member of A" and when I try to use A::A::foo() it complains about unresolved external.

How can I solve this? I think making a class inside the the same namespace is pretty stupid and leads to confusion but I'm not the author of the code and changing the namespace or the class name would cause too much trouble

Upvotes: 3

Views: 308

Answers (2)

Johan Lundberg
Johan Lundberg

Reputation: 27038

you can use :: to get to the top of the namespace hierarchy. So your class is ::A::A

edit: above answers your main question about namespaces. But as others pointed out, your problem is that you did not define foo. Add

inline void A::A::foo() { };

Upvotes: 3

Dietmar Kühl
Dietmar Kühl

Reputation: 153792

You want to use the full qualification, i.e. A::A::foo() (or even ::A::A::foo() if you insist on really complete qualification). The fact that the function is unresolved just means that you haven't defined it:

inline void A::A::foo() { ... }

... or skip the inline if the definitions in't in a header file.

BTW, I don't buy into your argument "causes too much trouble": if something is agreeably stupid (i.e. your team members agree that it is the wrong thing to do), fix the problem rather sooner than later! The longer you wait, the more problems it will cause, and the more trouble it becomes!

Upvotes: 1

Related Questions