繁体   English   中英

在文件之间的函数中传递结构:“预期的'struct Peer_Information *',但参数的类型为'struct Peer_Information *”

[英]Pass Struct in functions across files: “expected ‘struct Peer_Information *’ but argument is of type ‘struct Peer_Information *”

我尝试在多个文件中使用struct peer_struct.h。 它在main.c中声明,并且我传递了结构槽引用。

编译器使我警惕这些功能,我真的不明白为什么。

编译器警告:

In file included from first_use.c:1:0:
second_use.h:1:51: warning: ‘struct Peer_Information’ declared inside parameter list will not be visible outside of this definition or declaration
 void second_use(char *message, int number, struct Peer_Information *peer);
                                                   ^~~~~~~~~~~~~~~~
first_use.c: In function ‘first_use’:
first_use.c:6:23: warning: passing argument 3 of ‘second_use’ from incompatible pointer type [-Wincompatible-pointer-types]
     second_use(me, 5, peer);
                       ^~~~
In file included from first_use.c:1:0:
second_use.h:1:6: note: expected ‘struct Peer_Information *’ but argument is of type ‘struct Peer_Information *’
 void second_use(char *message, int number, struct Peer_Information *peer);
      ^~~~~~~~~~
In file included from main.c:4:0:
first_use.h:1:23: warning: ‘struct Peer_Information’ declared inside parameter list will not be visible outside of this definition or declaration
 void first_use(struct Peer_Information *peer);
                       ^~~~~~~~~~~~~~~~
main.c: In function ‘main’:
main.c:28:15: warning: passing argument 1 of ‘first_use’ from incompatible pointer type [-Wincompatible-pointer-types]
     first_use(&peer);
               ^
In file included from main.c:4:0:
first_use.h:1:6: note: expected ‘struct Peer_Information *’ but argument is of type ‘struct Peer_Information *’
 void first_use(struct Peer_Information *peer);

peer_struct.h

struct Peer_Information {
    char ownIP[16];
    char ownPort[6];
    unsigned int ownID;
    char successor_IP[16];
    char successor_Port[6];
    unsigned int successor_ID;
    char predecessor_IP[16];
    char predecessor_Port[6];
    unsigned int predecessor_ID;
} Peer_Information;

main.c中

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "first_use.h"
#include "peer_struct.h"

void init_peer(char *argv[], struct Peer_Information *peer){
//Fills struct up, not import for question
}

int main(int argc, char *argv[]) {
    if (argc != 10) {
        printf("Nicht genügend Parameter \n");
        return -1;
    }
    struct Peer_Information peer;
    init_peer(argv, &peer);
    first_use(&peer);
    return 0;
}

first_use.c

#include "second_use.h"
#include "peer_struct.h"

void first_use(struct Peer_Information *peer) {
    char me[] = "Hello";
    second_use(me, 5, peer);
}

first_use.h

void first_use(struct Peer_Information *peer);

second_use.c

#include <stdio.h>
#include <stdlib.h>
#include "peer_struct.h"

void second_use(char *message, int number, struct Peer_Information *peer) {
    printf("%d", peer->ownID);
}

second_use.h

void second_use(char *message, int number, struct Peer_Information *peer);

最初的警告说明了一切。 main.c包含first_use.h时,唯一包含的是该函数的原型

void first_use(struct Peer_Information *peer);

类型Peer_Information尚未定义,因此编译器生成的名称与该函数本地的类型相同。 没有什么比它应有的效果好了,因为您最终使用不同的(从编译器的角度来看)类型恰好具有相同的名称来调用该函数。

要修复它,您应该养成包括头文件所需的所有头文件的习惯。 在您的情况下,还包括类型的定义,并使first_use.h如下所示:

#include "peer_struct.h"
void first_use(struct Peer_Information *peer);

除此之外,您的包含文件会丢失所谓的包含保护。 您可以在此处获得有关这些内容的更多详细信息: C包括守卫到底是做什么的?

暂无
暂无

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

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