繁体   English   中英

将struct指针作为参数传递

[英]Passing struct pointer as a parameter

我正在尝试在c中编译此代码。 首先,我将这个结构放在一个单独的源文件中,以便像“类”一样使用它(dir.h)

//Structure
typedef struct s_dirobject {
    int noteid;
    char title[20];
    int bytes;
    char head[20];
    bool is_dir;
    struct s_dirobject* next;
} dirobject;

//Ops
void add_dirobject(dirobject* myDirobject,int num_dirobject, char title[20], int is_dir,     int bytes, char head[20]);
int get_dirobject_noteid(dirobject* myDirobject,int num_note);
char* get_dirobject_title(dirobject* myDirobject,int num_note);
int get_dirobject_bytes(dirobject* myDirobject,int num_note);
char* get_dirobject_head(dirobject* myDirobject,int num_note);
bool isdir(dirobject* myDirobject, int num_note);
int get_dirobjects_len(dirobject* myDirobject);
void clear_dir(dirobject* myDirobject);
void init_dir(dirobject* myDirobject);

在第二位,我有通信源从远程文件系统检索目录的内容,并填充对象(comms.c)

#include "notebook.h"
#include "dir.h"

dirobject* temporaldirobjects;

...

init_dir(temporaldirobjects);
while(cond) {
    //Add retrieved item to the directory
    add_dirobject(temporaldirobjects, index, title, is_dir, bytes, "");
}
//When done retrieving the contents from the source i do instantiate the notebook_window
notebook_init(source, path, temporaldirobjects);

最后,我的笔记本窗口界面将如下所示。 (notebook.h)

#include "dir.h"
void notebook_init(char* source, char* path, dirobject* contents);
void notebook_deinit();

及其实现(notebook.c)

void notebook_init(char* source, char* path, dirobject* contents) {

    // Fill the vars
    this_window_source=source;
    this_window_path=path;
    this_window_dirobjects=contents;

    ...
}

当我按原样编译此代码时,我得到错误说

../src/dir.h:13:16: error: redefinition of 'struct s_dirobject'
In file included from ../src/notebook.h:12:0,
                 from ../src/comms.c:25:
../src/dir.h:13:16: note: originally defined here
In file included from ../src/comms.c:27:0:
../src/dir.h:20:3: error: conflicting types for 'dirobject'
In file included from ../src/notebook.h:12:0,
                 from ../src/comms.c:25:
../src/dir.h:20:3: note: previous declaration of 'dirobject' was here
In file included from ../src/comms.c:27:0:
../src/dir.h:23:6: error: conflicting types for 'add_dirobject'
In file included from ../src/notebook.h:12:0,
                 from ../src/comms.c:25:
../src/dir.h:23:6: note: previous declaration of 'add_dirobject' was here
...

因为comms库包含dir.h和notebook.h,而notebook.h也是如此。 如果我删除notebook.hi中的包含此错误:

In file included from ../src/comms.c:25:0:
../src/notebook.h:14:46: error: unknown type name 'dirobject'

我怎么能做到这一点? 我想尽可能保持干净的代码。

您在comms.c文件中包含两个标头

#include "notebook.h"
#include "dir.h"

header notebook.h依次包含头文件dir.h

#include "dir.h"
void notebook_init(char* source, char* path, dirobject* contents);
void notebook_deinit();

结果,结构在同一编译单元中定义了两次。 您必须提供每个标头在每个编译单元中只包含一次。 要做到这一点,你必须提供标题的quars。 例如

#ifndef DIR_H
#define DIR_H

//Structure
typedef struct s_dirobject {
    int noteid;
    char title[20];
    int bytes;
    char head[20];
    bool is_dir;
    struct s_dirobject* next;
} dirobject;

//...

#endif

或者,如果编译器支持#pragme一次,那么您可以使用它。

通常,c中的多个声明很好,但多个定义不是。 在您的代码中,您多次包含dir.h ,导致重新定义struct s_dirobject 阅读有关“Header guard”或“Include Guard”的内容。 在这里 希望这可以通过重新定义解决您的主要问题。

暂无
暂无

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

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