3

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

ibrabeicker
  • 1,786
  • 2
  • 19
  • 31
  • Possible duplicate of [Classes and namespaces sharing the same name in C++](http://stackoverflow.com/questions/4070915/classes-and-namespaces-sharing-the-same-name-in-c). –  Jan 21 '12 at 21:24
  • 5
    The error about an unresolved external means that you didn't implement `A::A::foo()` rather than the syntax is wrong - in fact, the compiler has found what you wanted! Have you implemented, compiled, and linked in the implementation of `foo`? – templatetypedef Jan 21 '12 at 21:26
  • Here is an example of using `A::A::foo()` with an implementation of `foo` on codepad: http://codepad.org/3muVCfgM – Josh Peterson Jan 21 '12 at 21:32

2 Answers2

3

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() { };

Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
1

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!

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380