我有一个结构,它有一个函数指针作为成员变量。函数指针将指向struct的指针作为其参数之一。
通过查看此处发布的解决此类循环依赖关系的解决方案,我使用了前向声明来编译。
我仍然遇到无法初始化结构的问题。下面发布的是代码和输出。
编辑:我早期的MVCE没有正确地重现问题。我可以通过命名结构而不是仅仅具有结构别名来编译它。虽然不太确定原因。
#include "stdio.h"
//Forward declaration
struct data;
typedef int(*funcPtr) (struct data* a);
typedef struct
{
int a;
int b;
funcPtr foo;
}data;
int foo(data* pData)
{
return 0;
}
static data testData[] = {{1,1,foo}, {0,2,foo},};
int main()
{
printf("Just trying to compile this program\n");
return 0;
}
错误输出 -
compileError.c:20:1: warning: initialization from incompatible pointer type [enabled by default]
static data testData[] = {{1,1,foo}, {0,2,foo},};
^
compileError.c:20:1: warning: (near initialization for \u2018testData[0].foo\u2019) [enabled by default]
compileError.c:20:1: warning: initialization from incompatible pointer type [enabled by default]
compileError.c:20:1: warning: (near initialization for \u2018testData[1].foo\u2019) [enabled by default
答案 0 :(得分:2)
功能foo
与funcPtr
不兼容 - 它会返回int
,而不是void
。
此外,您的变量被声明为数组,但您使用单个值初始化它 - 删除[]
。
答案 1 :(得分:1)
您定义了一个struct数据数组,但只用一个元素初始化它。如果这是无意的,请删除' []':
data testData[] = { {1,1,foo} };
如果需要数组,请使用完全支撑的数组初始值设定项进行初始化:
void
另外,检查您的会员功能签名。声明和实现都应返回int
或<script>
$(document).bind('mobileinit',function(){
$.mobile.changePage.defaults.changeHash = false;
$.mobile.hashListeningEnabled = false;
$.mobile.pushStateEnabled = false;
});
</script>
。