Irbis77
Irbis77

Reputation: 173

Inheritance for class with the same name but different namespace c++

Is it possible to inherit from the class with the same name, but in different namespace and how to achieve it?

For example I have the following structure:

   namespace general {
           namespace gui {
             struct GUI {

        };
   }}

can I do:

 namespace proxy {
          namespace gui {
            struct GUI : general::gui::GUI {
    
          };
     }}

Or / and there should be another way to doing this (short of actually naming the class / structure differently)?

Update based on Pete's Becker comment.

The current way it is structured is like that:

gui.h

    #include "world.h"
     
namespace general {
               namespace gui {
                 struct GUI {
    
            };
   }}

world.h

namespace general {

    namespace world {

        struct World {};
}}

proxy.h

 namespace proxy {
          namespace gui {
            struct GUI : general::gui::GUI {

          };
     }}

My understanding is that compiler looks at world.h, doesn't find general::gui and complains with

proxy.h(17,30): error C2039: 'gui': is not a member of 'general'
world.h(4): message : see declaration of 'general'

If I add

namespace world {
    
            struct World {};
    }}

to gui.h or other way around, it will most probably work. Just was confused of why do I need to do it.

Update2 (with .cpp files):

world.cpp

#include "world.h"

namespace general {
    namespace world {
                    World{

gui.cpp

#include "gui.h"

namespace general {
    namespace gui {

proxy.cpp

#include "proxy.h"

namespace proxy {
    namespace gui {

On the .cpp files side, all of them are just including their corresponding header files. This is the general rule.

Plus here is the (relevant) header file chain, which most certainly where the problem is:

goal.h    includes      world.h and proxy.h
gui.h     includes      world.h and goal.h
menu.h    includes      goal.h
world.h - includes nothing
proxy.h - includes nothing

Update3: Fixed now by moving proxy.h into gui.h temporarily and in the process uncovering a dependency error. Will move the files back and just wanted to confirm that the issue was with the interdependencies of the .h files.

Upvotes: 0

Views: 1148

Answers (1)

Hajo Kirchhoff
Hajo Kirchhoff

Reputation: 2075

Yes, this is possible. If you are getting an error, there is something in your code you have not posted here, such as a using namespace statement.

proxy::gui::GUI and general::gui::GUI are two entirely different classes. The fact that both end with GUI says nothing at all.

The full/complete name of the class is always the full name including namespaces.

That's the point of namespaces. They were invented to prevent name clashes between different libraries. Imagine you have library of vendor "A" which includes a class GUI. Imagine you want to use this library in your own code, where you also have defined a class GUI. Without namespaces these names would clash and you would be forced to rename your own class to use the library.

But with namespaces all is good. A::GUI is something different than yourcode::GUI or just plain GUI.

So

namespace general { namespace gui {
   class GUI {};
}}

namespace proxy { namespace gui {
   class GUI:public general::gui::GUI {};
}}

will / should work.


Update (after the .cpp has been posted)

  • proxy.cpp includes proxy.h
  • proxy.h refers to general::gui::GUI

but general::gui is not visible to proxy.h. It has not been included. Neither proxy.cpp nor proxy.h "known" anything about a general or a general::gui.

That is exactly what the compiler error message is saying.

proxy.h(17,30): error C2039: 'gui': is not a member of 'general'

The gui thing you are trying to access (in the inheritance) is not a member of a general thing -- at least not to proxy.cpp or proxy.h. These two files know only about a namespace proxy and a namespace gui. Nothing else. Hence the error.

It is not enough to declare a namespace general or namespace gui in any header file. Header files don't do anything by themselves.

Add a

#include "gui.h"

to your proxy.h and it should work.

But beware of circular references.

Read more about includes (and include guards) before continuing.

Upvotes: 3

Related Questions