OS = linux (Fuduntu 2013.2)
i am trying to use GList in my program but when i call the g_list_append i get this error
error: invalid conversion from ‘const void*’ to ‘gpointer {aka void*}’ [-fpermissive]
In file included from /usr/include/glib-2.0/glib/ghash.h:35:0,
from /usr/include/glib-2.0/glib.h:52,
from /usr/include/glib-2.0/gobject/gbinding.h:30,
from /usr/include/glib-2.0/glib-object.h:25,
from /usr/include/glib-2.0/gio/gioenums.h:30,
from /usr/include/glib-2.0/gio/giotypes.h:30,
from /usr/include/glib-2.0/gio/gio.h:28,
from /usr/include/gtk-2.0/gdk/gdkapplaunchcontext.h:30,
from /usr/include/gtk-2.0/gdk/gdk.h:32,
from /usr/include/gtk-2.0/gtk/gtk.h:32,
from main.cpp:4:
/usr/include/glib-2.0/glib/glist.h:61:10: error: initializing argument 2 of ‘GList* g_list_append(GList*, gpointer)’ [-fpermissive]
here is the part of the code witch contains g_list_append
#include<iostream>
#include <stdio.h>
#include<gtk/gtk.h>
#include<glibmm.h>
int main(){
GList *glist_forleg = NULL;
glist_forleg = g_list_append(glist_forleg, "A1");
return 0;
}
compiled with
g++ -o kabel main.cpp strukt.cpp -lm -Wall `pkg-config --cflags --libs glibmm-2.4` `pkg-config --cflags --libs gtk+-2.0`
This isn't a GLib problem (although you should probably use the C++ classes rather than the C versions), it's rather because quoted strings are
const
. The function you're trying to use takes avoid *
pointer rather than aconst void *
pointer, which is what the error is trying to tell you.Here's a fixed example:
...that casts the string to
gpointer
(akavoid *
) rather thanconst void *
. Compile e.g. with:However, this is a bad idea as the string won't necessarily stick around in memory, causing the pointer to point to memory that is not yours, and cause a memory fault. A better idea (provided that you remember to free it later) might be to use
g_strdup
which would duplicate the string in memory, or just to use the GLibmm-provided types as explained in the C++ version of the library's documentation.