1

I'm trying to compile some code and I'm getting the following error:

error: invalid use of member (did you forget the ‘&’ ?)

This is coming from the g_signal_connect call:

g_signal_connect ((gpointer) Drawing_Area_CPU, "expose-event", 
        G_CALLBACK (graph_expose), NULL);

Drawing_Area_CPU is a GtkWidget * and graph_expose is defined as:

gboolean graph_expose(GtkWidget *widget, GdkEventExpose *event, gpointer data);

So far as i can tell im doing everything right, but still i get this error. Can anyone help please?

UPDATE:

Sorry guys, i got confused, my graph_expose function is in a class, and I'm trying to do the g_signal_connects from the constructor of that class, would that affect this problem in any way?

iain
  • 5,660
  • 1
  • 30
  • 51
paultop6
  • 3,691
  • 4
  • 29
  • 37

1 Answers1

4

As GTK+ is written in plain C, callbacks have to be either plain functions or static methods, so if you want to be able to use class methods as callbacks, you must use some kind of static proxy method:

class foo {
    foo () {
        g_signal_connect (GtkWidget *widget, GdkEventExpose *event,
                          G_CALLBACK (foo::graph_expose_proxy), this);
    }

    gboolean graph_expose (GtkWidget *widget, GdkEventExpose *event) {
        // sth
    }

    static gboolean graph_expose_proxy (GtkWidget *widget, GdkEventExpose *event, gpointer data) {
        foo *_this = static_cast<foo*>(data);
        return _this->graph_expose (widget, event);
    }
}

Or, alternatively, you could use GTKmm, which is C++ binding for GTK+.

  • Unless you want your code to break unexpectedly, use free functions when you need C-callbacks - for why see e.g. here: http://stackoverflow.com/questions/2068022/in-c-is-it-safe-portable-to-use-static-member-function-pointer-for-c-api-callb – Georg Fritzsche Apr 25 '10 at 17:29