简体   繁体   English

为什么在if语句中初始化了function?

[英]Why is the function initialized in the if statement?

For some reason, I'm getting an error that says conflicting types for 'get_property' because the function is reinitialized below.出于某种原因,我收到一个错误,提示conflicting types for 'get_property'因为 function 在下面重新初始化。 Why is this happening and how can I fix this?为什么会发生这种情况,我该如何解决?

/* find the longest client_machine name */
for (i = 0; i < client_list_size / sizeof(Window); i++)
{
    gchar *client_machine;
    if ((client_machine = get_property(disp, client_list[i],
                                       XA_STRING, "WM_CLIENT_MACHINE", NULL)))
    {
        max_client_machine_len = strlen(client_machine);
    }
    g_free(client_machine);
}

Details of the error:错误详情:

/home/raphael/Desktop/Projects/sus/util.h:134:8: error: conflicting types for ‘get_property’
  134 | gchar *get_property(Display *disp, Window win,
      |        ^~~~~~~~~~~~
/home/raphael/Desktop/Projects/sus/util.h:64:31: note: previous implicit declaration of ‘get_property’ was here
   64 |         if ((client_machine = get_property(disp, client_list[i],
      |                               ^~~~~~~~~~~~

What you need is a function prototype in the beginning of the file (before line 64, where you are calling it inside that if ).您需要的是文件开头的 function原型(在第 64 行之前,您在if中调用它)。

In computer programming, a function prototype or function interface is a declaration of a function that specifies the function's name and type signature, but omits the function body. In computer programming, a function prototype or function interface is a declaration of a function that specifies the function's name and type signature, but omits the function body. While a function definition specifies how the function does what it does (the "implementation"), a function prototype merely specifies its interface, ie what data types go in and come out of it. While a function definition specifies how the function does what it does (the "implementation"), a function prototype merely specifies its interface, ie what data types go in and come out of it.

In short, the compiler does not know what your function returns or expects as parameters when you call it in line 64. So, in order to inform the compiler on how to interpret your function, put this somewhere in the beginning of the file, ideally right after include or struct definitions, along with global variables:简而言之,当您在第 64 行调用它时,编译器不知道您的 function 作为参数返回或期望作为参数。因此,为了告知编译器如何解释您的 function,最好将其放在文件开头的某个位置在includestruct定义之后,以及全局变量:

 gchar *get_property(Display *disp, Window win, ... )

Note that for this to work, the gchar type must have been defined already (you can not use a type if you have not defined the type yet...).请注意,要使其正常工作,必须已经定义了gchar类型(如果尚未定义类型,则不能使用类型......)。 In general, I try to have a file structure similar to this:一般来说,我尝试使用类似于以下的文件结构:

1. Includes
2. Defines
3. Structs, enuns and unions
4. Global variables
5. Externs
6. Prototypes
7. Functions definitions

By the way, nothing wrong with the if .顺便说一句, if没有错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM