简体   繁体   English

当我从 main 中拆分函数时,struct 出现问题

[英]Problem with struct when I split the functions from main

I have a struct in my main let's say:我的main结构中有一个结构,让我们说:

struct node
{
    char name[N];
    int num;
    char **group;
    int groupsize;
    struct node * next;
};

and I have my functions to other .c and .h files call myfuncs.ch我有其他 .c 和 .h 文件的函数调用 myfuncs.ch

myfuncs.h myfuncs.h

void create_node(struct node **, struct node **, int, int, char **, int);
void display(struct node *head);
void start_joseph(struct node **, int, int);

and myfuncs.c和 myfuncs.c

void create_node(struct node **, struct node **, int, int, char **, int);
{
  do something;
}

void display(struct node *head);
{
  do something;
}

void start_joseph(struct node **, int, int);
{
  do something;
}

but I get this error但我收到这个错误

[Warning] 'struct node' declared inside parameter list
[Warning] its scope is only this definition or declaration, which is probably not what you want

Should I define the struct to the pre-processor or what?我应该为预处理器定义结构还是什么?

Put the structure definition in the header before the function declarations that use it.将结构定义放在使用它的函数声明之前的头文件中。

At minimum, you need a line:至少,您需要一行:

struct node;

in the header before the function declarations.在函数声明之前的头文件中。 Since you don't appear to be trying to create an opaque type (the details of which would be hidden from the code using it), then embedding the entire structure definition is reasonable.由于您似乎没有尝试创建不透明类型(使用它的代码将隐藏其细节),因此嵌入整个结构定义是合理的。 Note that you then delete that definition from the code holding the main() function.请注意,您随后从包含main()函数的代码中删除了该定义。

What the error message says is that if you write:错误消息说的是,如果你写:

void create_node(struct node **, struct node **, int, int, char **, int);

and there is no previous mention of the type struct node , then the type name is created but only exists for that one function declaration.并且之前没有提到类型struct node ,然后类型名称被创建,但只存在于那个函数声明中。 The next function that refers to struct node creates a new type name struct node which is different from and incompatible with the previous one.下一个引用struct node函数会创建一个新的类型名称struct node ,它与前一个不同且不兼容。 By declaring struct node;通过声明struct node; at file scope, you're saying "there is a type struct node ".在文件范围内,您说的是“有一个类型struct node ”。 Basically, you have to have something outside the scope of a prototype that says "there is a type struct node " which the function prototype can then use.基本上,你必须有一个原型范围之外的东西,上面写着“有一个类型struct node ”,然后函数原型可以使用它。

Suggested solution — myfuncs.h :建议的解决方案 - myfuncs.h

#ifndef MYFUNCS_H_INCLUDED
#define MYFUNCS_H_INCLUDED

struct node
{
    char name[N];
    int num;
    char **group;
    int groupsize;
    struct node * next;
};

extern void create_node(struct node **, struct node **, int, int, char **, int);
extern void display(struct node *head);
extern void start_joseph(struct node **, int, int);

#endif /* MYFUNCS_H_INCLUDED */

The header guards prevent problems if the file is included multiple times in a single translation unit (source file plus included headers).如果文件在单个翻译单元(源文件加上包含的标题)中多次包含,则标题保护可以防止出现问题。 I use extern on function declarations to match the extern needed on variables declared in the header;我在函数声明中使用extern来匹配头文件中声明的变量所需的extern many people don't.很多人没有。 That choice is up to you and/or the coding guidelines you work with.该选择取决于您和/或您使用的编码指南。 Removing the extern makes no difference to the result.删除extern对结果没有影响。

I think you should declare node struct at other header file, eg global.h, and include the global.h in myfuncs.h.我认为您应该在其他头文件中声明节点结构,例如 global.h,并在 myfuncs.h 中包含 global.h。

  // in the global.h 
 struct node
 {
      char name[N];
      int num;
      char **group;
      int groupsize;
      struct node * next;
  };
  ....    ....
 // in the myfuncs.h
 #include "global.h"

In these function declarations在这些函数声明中

void create_node(struct node **, struct node **, int, int, char **, int);
void display(struct node *head);
void start_joseph(struct node **, int, int);

the type struct node has the function prototype scopes of each function declaration and is not visible outside the parameter declaration lists.类型结构节点具有每个函数声明的函数原型范围,并且在参数声明列表之外不可见。

The same problem exist with the function definitions.函数定义也存在同样的问题。 And the warnings和警告

[Warning] 'struct node' declared inside parameter list
[Warning] its scope is only this definition or declaration, which is probably not what you want

point to this.指向这一点。

The declaration of the structure in main has the function main block scope and is also invisible outside the function. main 中结构体的声明具有函数 main 块作用域,并且在函数之外也是不可见的。

You need at least declare (or define) the structure before the function declarations and the structure shall be already defined before the function definitions.您至少需要在函数声明之前声明(或定义)结构,并且结构应该在函数定义之前已经定义。

So move the structure definition from main to a point before the function declarations.因此,将结构定义从 main 移到函数声明之前的某个点。

For example例如

myfuncs.h myfuncs.h

struct node
{
    char name[N];
    int num;
    char **group;
    int groupsize;
    struct node * next;
};

void create_node(struct node **, struct node **, int, int, char **, int);
void display(struct node *head);
void start_joseph(struct node **, int, int);

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

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