qt - How to handle namespaces in C++? -
i have created dialogbox in qtdesigner
//---------- *.h namespace ui { class mydialog; } class mydialog : public qdialog { q_object public: explicit mydialog(qwidget* parent = 0); ~mydialog(); private: ui::mydialog* ui; };
and source
// --------- *.cpp mydialog::mydialog(qwidget* parent = 0) : qdialog(parent), ui(new ui::mydialog) { ui->setupui(this); //.. }
i structure in namespace encapsulation, have com::example::mydialogs::mydialog like:
//---------- *.h namespace com { namespace example { namespace mydialogs { namespace ui { class mydialog; } class mydialog : public qdialog { q_object public: explicit mydialog(qwidget* parent = 0); ~mydialog(); private: ui::mydialog* ui; }; }}} //namespace closing
and source
// --------- *.cpp namespace com { namespace example { namespace mydialogs { mydialog::mydialog(qwidget* parent = 0) : qdialog(parent), ui(new ui::mydialog) { ui->setupui(this); //.. } }}} //namespace closing
but compiler complains message:
c:\myprojects\test\com\example\mydialogs\mydialog.h:29: error: forward declaration of 'class com::example::mydialogs::ui::mydialog' class mydialog;
the namespace ui added qt automatically. how can use namespace structure properly?
in order forward declare namespace, need use following canonical syntax:
namespace ns1 { namespace ns2 { //.... namespace nsn { class a; } //.... } }
the above code snippet taken here. in case, need close namespaces forward declaration is.
namespace com { namespace example { namespace mydialogs { namespace ui { class mydialog; } } // mydialogs } // example } // com namespace com { namespace example { namespace mydialogs { class mydialog : public qdialog { q_object public: explicit mydialog(qwidget* parent = 0); ~mydialog(); private: ui::mydialog* ui; }; }}} //namespace closing
your implementation code looks correct. make slight changes header file , should compile.
i have not tested code. use @ own risk.
Comments
Post a Comment