Re: RefPtr in constructor



On 2/1/07, Tomek Lorek <tlorek gmail com> wrote:
Hi there,
I'd like to divide my application into classes but have problems while
passing the RefPtr to the constructor. The main routine looks like:

int main(int argc, char *argv[])
{
   Gtk::Main kit(argc, argv);
   Glib::RefPtr<Gnome::Glade::Xml> refXml;
   try
      refXml = Gnome::Glade::Xml::create("myapp.glade");
   catch (const Gnome::Glade::XmlError& ex) {
      cerr << ex.what() << endl;
      return 1;
   }

   MyApp app(refXml);

   kit.run(app.get_window); // get_window is the MyApp's public method
returning the &m_wWindow
   return 0;
}

and the MyApp class:

6: MyApp::MyApp(Glib::RefPtr<Gnome::Glade::Xml> refXml)
7: {
8:    refXml->get_widget("wndMain", &m_wWindow);
9: }

And now, while compiling I get this:

$ make
g++ -Wall -Wno-deprecated -c  `pkg-config gtkmm-2.4 libglademm-2.4
libxml++-2.6 glibmm-2.4 --cflags` -D_GNU_SOURCE myapp.cpp
myapp.cpp: In constructor 'MyApp::MyApp(Glib::RefPtr<Gnome::Glade::Xml>)':
myapp.cpp:8: error: no matching function for call to
'Gnome::Glade::Xml::get_widget(const char [8], Gtk::Window*)'
/usr/include/libglademm-2.4/libglademm/xml.h:185: note: candidates
are: Gtk::Widget* Gnome::Glade::Xml::get_widget(const Glib::ustring&)
/usr/include/libglademm-2.4/libglademm/xml.h:201: note:
 T_Widget* Gnome::Glade::Xml::get_widget(const Glib::ustring&,
T_Widget*&) [with T_Widget = Gtk::Window]

Does it mean that I can't pass the RefPtr to the constructor? How else
can I do this?

Best regards,
teel

There's no problem passing the RefPtr to the constructor.   If you
notice the candidates that it offered in the error message, the second
one is the one that you are trying to use, and it requires a
reference-to-a-pointer-to-a-T_Widget.  i.e.:

T_Widget* Gnome::Glade::Xml::get_widget(const Glib::ustring&,
T_Widget*&) [with T_Widget = Gtk::Window]

You didn't provide the full source code for MyApp, but I assume that
m_wWindow is of type Gtk::Window.  What happens when you take the
address of m_wWindow is that it creates a temporary of type
Gtk::Window*, and temporaries cannot be passed as references.

This actually reveals an underlying problem in the above code: you are
passing in the address of an already allocated Gtk::Window object when
you should just be passing a unallocated pointer variable (since the
widget will be allocated inside of libglade).  So you should be able
to fix this easily by simply changing your m_wWindow variable to be of
type Gtk::Window* (i.e. a pointer).

--
jonner



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]