编译器类型警告和错误
问题描述:
if(xmlStrEquals(cur->name, (const xmlChar *) "check")) // Find out which type it is
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (gtk_builder_get_object (builder, xmlGetProp(cur,"name"))),(gboolean) xmlGetProp(cur,"value"));
else if(xmlStrEquals(cur->name, (const xmlChar *) "spin"))
gtk_adjustment_set_value(GTK_ADJUSTMENT (gtk_builder_get_object (builder, xmlGetProp(cur,"name"))),(gdouble) xmlGetProp(cur,"value"));
else if(xmlStrEquals(cur->name, (const xmlChar *) "combo"))
gtk_combo_box_set_active(GTK_COMBO_BOX (gtk_builder_get_object (builder, xmlGetProp(cur,"name"))),(gint) xmlGetProp(cur,"value"));
以下3个错误对应于上述3个if语句。编译器类型警告和错误
main.c:125: warning: cast from pointer to integer of different size
main.c:130: error: pointer value used where a floating point value was expected
main.c:139: warning: cast from pointer to integer of different size
如果你允许我提取违规部分:
(gboolean) xmlGetProp(cur,"value")
(gdouble) xmlGetProp(cur,"value")
(gint) xmlGetProp(cur,"value")
为什么这些类型转换导致这些错误?我该如何解决它们?
尝试使用(gboolean *)
等从GTK的线沿线收到警告:
warning: passing argument 2 of ‘gtk_toggle_button_set_active’ makes integer from pointer without a cast
/usr/include/gtk-2.0/gtk/gtktogglebutton.h:82: note: expected ‘gboolean’ but argument is of type ‘gboolean *’
error: incompatible type for argument 2 of ‘gtk_adjustment_set_value’
/usr/include/gtk-2.0/gtk/gtkadjustment.h:93: note: expected ‘gdouble’ but argument is of type ‘gdouble *’
warning: passing argument 2 of ‘gtk_combo_box_set_active’ makes integer from pointer without a cast
/usr/include/gtk-2.0/gtk/gtkcombobox.h:99: note: expected ‘gint’ but argument is of type ‘gint *’
答
我固定它,像这样:
(gboolean) *xmlGetProp(cur,"value")
(gdouble) *xmlGetProp(cur,"value")
(gint) *xmlGetProp(cur,"value")
仍然不知道为什么工作,但我能猜到。
答
的返回一个字符串(如xmlChar *
):
搜索并获得相关的属性值到一个节点这是实体替代。该函数在DTF属性声明中查找#FIXED或默认声明值,除非DTD使用已被关闭。
[...]
返回:属性值或NULL,如果没有找到。这取决于调用者使用xmlFree()释放内存。
调用者负责将该字符串解析为任何形式(浮点,整数,布尔,...)。另请注意,主叫方也负责释放返回的xmlChar *
字符串。
解决您的问题,您需要将字符串转换为你通常的方式所需要的。并且不要忘记xmlFree
返回的字符串。
抱歉,我以为'(类型)variable'是用通常的方法? – 2011-04-29 20:40:41