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 a void *
pointer rather than a const void *
pointer, which is what the error is trying to tell you.
Here's a fixed example:
#include <glib.h>
int main(){
GList *list = NULL;
list = g_list_append(list, (gpointer)"A1");
return 0;
}
...that casts the string to gpointer
(aka void *
) rather than const void *
. Compile e.g. with:
cc -Wall -o test test.c $(pkg-config --cflags --libs glib-2.0)
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.