Search code examples
c++namespaces

Problems with accesing elements outside nested namespace C++


I have this big namespace for my entire Project and then each componenet (file) has its own nested namespace inside that file. The big namespace is called IEEngine and the nested ones are called IEWhatever (IEWindow for example). But how would i from IEEngine::IEWhatever2 access IEEngine::IEWhatever::Something (something is a class). When i do IEEngine::IEWhatever::Something i get a error that no member IEWhatever in namespace IEEngine. i included the file that provides the namespace IEWWhatever. but that file also includes the file that provides IEWhatever2 so it errors saying that IEWhatever is not defined (the include is put before the IEWhatever namespace). How would i go about fixing this IESomething::Something also doesnt work. I also tried using Something = IEWhatever::Something; but that didnt work

IEDisplay.hpp:

#ifndef IEDISPLAY_HPP
#define IEDISPLAY_HPP

#ifndef IEWINDOW_HPP
#include "IEWindow.hpp"
#endif // IEWINDOW_HPP


#include <wayland-client.h>
#include <wayland-cursor.h>

#include "external/wayland-protocols/tearing-control-v1/tearing-control-v1.h"
#include "external/wayland-protocols/xdg-shell/xdg-shell-client-protocol.h"
#include "external/wayland-protocols/viewporter/viewporter-client-protocol.h"



namespace IEEngine {
    namespace IEDisplay {
        class WLDisplay {   
            private:
                struct wl_display* display;
                struct wl_registry* registry;
                struct wl_compositor* compositor;
                struct xdg_wm_base* wm_base;
                struct wl_seat* seat;
                struct wl_pointer* pointer;
                struct wl_touch* touch;
                struct wl_keyboard* keyboard;
                struct wl_shm* shm;
                struct wl_cursor_theme* cursor_theme;
                struct wl_cursor* cursor;
                struct wl_surface* cursor_surface;
                struct wp_tearing_control_manager_v1* tearing_manager;
                struct wp_viewporter* viewporter;
                struct wp_fractional_scale_manager_v1* fractional_scale_manager;
                IEWindow::WLWindow window;
        };
    }
}


#endif // IEDISPLAY_HPP

IEWindow.hpp:

#ifndef IEWINDOW_HPP
#define IEWINDOW_HPP




#ifndef IEDISPLAY_HPP
#include "IEDisplay.hpp"
#endif // IEDISPLAY_HPP

namespace IEEngine {
    namespace IEWindow {
        class WLWindow {    
            IEDisplay::WLDisplay display;
            int wHeight;
            int wWidth;
        };
    }
}



#endif // IEWINDOW_HPP

Solution

  • Based on the code provided, you need to add a forward declaration for WLDisplay. Here is an abbreviated version with most of the details elided.

    namespace IEEngine {
       namespace IEWindow {
          class IEDisplay::WLDisplay; // <<== added
          class WLWindow { 
             /*...*/
             IEDisplay::WLDisplay display;
          };
       }
    }
    
    namespace IEEngine {
       namespace IEDisplay {
          class WLDisplay {
          private:
             IEWindow::WLWindow& window;
             /*...*/
          };
       }
    }
    

    Note also the use of a reference for IEWindow::WLWindow& window in WLDisplay since I suspect you don't really want to include the class there.